简体   繁体   English

DateTimeFormatter 无法按预期工作

[英]DateTimeFormatter doesn't work as expected

I have to convert date-time string to a zoned date-time object.我必须将日期时间字符串转换为分区日期时间 object。 I used DateTimeFormatter to read the pattern.我使用DateTimeFormatter来读取模式。 According to the documentation , the "Z" in the pattern can accept format like:根据文档,模式中的“Z”可以接受如下格式:

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

But ZonedDateTime.parse(myDate, formatter) works only for first case;但是ZonedDateTime.parse(myDate, formatter)仅适用于第一种情况; 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:我正在使用 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();

What am I doing wrong?我究竟做错了什么? Thanks!谢谢!

You specified Z for the timezone so that's why 2020-06-08T12:59:10.288+0000 works.您为时区指定了 Z,这就是 2020-06-08T12:59:10.288+0000 有效的原因。

But if you want to parse 2020-06-08T12:59:10.288+00:00 your format must be.但是如果你想解析 2020-06-08T12:59:10.288+00:00 你的格式必须是。

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

You can find this in the JavaDoc:您可以在 JavaDoc 中找到它:

Offset Z: This formats the offset based on the number of pattern letters.偏移 Z:这会根据图案字母的数量来格式化偏移。 One, two or three letters outputs the hour and minute, without a colon, such as '+0130'.一个、两个或三个字母输出小时和分钟,不带冒号,例如'+0130'。 The output will be '+0000' when the offset is zero.当偏移量为零时,output 将为“+0000”。 Four letters outputs the full form of localized offset, equivalent to four letters of Offset-O.四个字母输出本地化偏移的完整形式,相当于四个字母的Offset-O。 The output will be the corresponding localized offset text if the offset is zero.如果偏移量为零,则 output 将是相应的本地化偏移量文本。 Five letters outputs the hour, minute, with optional second if non-zero, with colon.五个字母输出小时、分钟,如果非零,则可选秒,带冒号。 It outputs 'Z' if the offset is zero.如果偏移量为零,则输出“Z”。 Six or more letters throws IllegalArgumentException.六个或更多字母会引发 IllegalArgumentException。

You do need to define a format for the date-time string, 2020-06-08T12:59:10.288+00:00 .您确实需要为日期时间字符串2020-06-08T12:59:10.288+00:00定义格式。 It is already in the default format of OffsetDateTime .它已经是 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: 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.注意:ZonedDateTimeLocalDateTime的转换会丢弃有价值的信息,即时区。 Therefore, you should perform this conversion only when you are sure that your business logic will not require the time-zone information.因此,只有当您确定您的业务逻辑不需要时区信息时,您才应该执行此转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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