Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.time.format.DateTimeParseException: Text '1963-03-24T00:00:00.000+01:00' could not be parsed at index 19 1000

Tags:

date

java-8

I get the datetime value as: 1963-03-24T00:00:00.000+01:00

I am using Java 8 to parse the date time as following:

public static final DateTimeFormatter ISO_OFFSET_DATE_TIME = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX ZD");

public static ZonedDateTime parseDateTime(String date) {

    return parseDateTime(date, ISO_OFFSET_DATE_TIME);
}
public static ZonedDateTime parseDateTime(String date, DateTimeFormatter format) {
    if (date == null) {
        return null;
    }

    return ZonedDateTime.parse(date, format);
}

When I run this, I get this error:

    Caused by: java.time.format.DateTimeParseException: Text '1963-03-24T00:00:00.000+01:00' could not be parsed at index 19
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949) ~[?:1.8.0_112]
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851) ~[?:1.8.0_112]
at java.time.ZonedDateTime.parse(ZonedDateTime.java:597) ~[?:1.8.0_112]

What's wrong with my pattern ?

like image 799
Laetitia28 Avatar asked Nov 22 '25 13:11

Laetitia28


1 Answers

Use the following Date Formatter:

DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");

instead of:

DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX ZD");

The last part of your time input is .000+0100 which comprises of a dot, milliseconds & timezone.

The dot can be represented as it is i.e. .

The millis .000 can be parsed by .SSS, and the timezone +01:00 can be parsed by XXX.

like image 167
Pankaj Singhal Avatar answered Nov 25 '25 00:11

Pankaj Singhal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!