有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java DateTimeFormatter无法按预期工作

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

  • +/-万
  • +/-00:00

但是分区时间。parse(myDate,格式化程序)仅适用于第一种情况;相反,在第二种情况下,代码生成一个异常

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

我正在使用Java8。举例及;代码:

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

我做错了什么? 谢谢


共 (2) 个答案

  1. # 1 楼答案

    您为时区指定了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.

  2. # 2 楼答案

    您确实需要为日期时间字符串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的转换丢弃了有价值的信息,即时区。因此,只有在确定业务逻辑不需要时区信息时,才应该执行此转换