DateTimeFormatter 没有按预期工作

DateTimeFormatter doesn't work as expected

我必须将日期时间字符串转换为分区日期时间对象。我使用 DateTimeFormatter 来读取模式。根据documentation,pattern中的"Z"可以接受的格式如下:

但是 ZonedDateTime.parse(myDate, formatter) 仅适用于第一种情况;而在第二种情况下,代码会生成异常。

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

我正在使用 Java 8. 示例和代码:

"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();

我做错了什么? 谢谢!

您为时区指定了 Z,这就是 2020-06-08T12:59:10.288+0000 起作用的原因。

但是如果你想解析 2020-06-08T12:59:10.288+00:00 你的格式必须是。

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

您可以在 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.

您确实需要为日期时间字符串定义格式,2020-06-08T12:59:10.288+00:00。它已经在 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);
    }
}

输出:

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

注意:ZonedDateTimeLocalDateTime 的转换丢弃了有价值的信息,即时区。因此,只有当您确定您的业务逻辑不需要时区信息时,您才应该执行此转换。