简体   繁体   中英

DateTimeFormatter doesn't work as expected

I have to convert date-time string to a zoned date-time object. I used DateTimeFormatter to read the pattern. According to the documentation , the "Z" in the pattern can accept format like:

  • +/- 0000
  • +/- 00:00

But ZonedDateTime.parse(myDate, formatter) works only for first case; instead in the second case code generates an exception.

Execution exception[[DateTimeParseException: Text '2020-06-22T16:00:00.000+00:00' could not be parsed at index 23]]

I'm using Java 8. Example & code:

"2020-06-08T12:59:10.288+0000" **work**
"2020-06-08T12:59:10.288+00:00" **doesn't work**

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

ZonedDateTime dateConvertedUTC = ZonedDateTime.parse(dateTime, formatter);
LocalDateTime dateConverted = dateConvertedUTC.withZoneSameInstant(ZoneId.of("Europe/Rome")).toLocalDateTime();

What am I doing wrong? Thanks!

You specified Z for the timezone so that's why 2020-06-08T12:59:10.288+0000 works.

But if you want to parse 2020-06-08T12:59:10.288+00:00 your format must be.

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

You can find this in the JavaDoc:

Offset Z: This formats the offset based on the number of pattern letters. One, two or three letters outputs the hour and minute, without a colon, such as '+0130'. The output will be '+0000' when the offset is zero. Four letters outputs the full form of localized offset, equivalent to four letters of Offset-O. The output will be the corresponding localized offset text if the offset is zero. Five letters outputs the hour, minute, with optional second if non-zero, with colon. It outputs 'Z' if the offset is zero. Six or more letters throws IllegalArgumentException.

You do need to define a format for the date-time string, 2020-06-08T12:59:10.288+00:00 . It is already in the default format of OffsetDateTime .

import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class Main {
    public static void main(String[] args) {
        OffsetDateTime odt = OffsetDateTime.parse("2020-06-08T12:59:10.288+00:00");
        System.out.println(odt);

        // Get ZonedDateTime in the desired time-zone from OffsetDateTime
        ZonedDateTime zdt = odt.atZoneSameInstant(ZoneId.of("Europe/Rome"));

        // Get LocalDateTime from ZonedDateTime
        LocalDateTime ldt = zdt.toLocalDateTime();
        System.out.println(ldt);
    }
}

Output:

2020-06-08T12:59:10.288Z
2020-06-08T14:59:10.288

Note: The conversion from ZonedDateTime to LocalDateTime throws away the valuable information, the time-zone. Therefore, you should perform this conversion only when you are sure that your business logic will not require the time-zone information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM