【问题标题】:Local datetime parsing本地日期时间解析
【发布时间】:2020-02-05 02:34:04
【问题描述】:

我正在尝试将输入字符串解析为本地日期时间。

下面是我的一段代码

 ZonedDateTime z = ZonedDateTime.parse("2019-11-26T19:30:00Z", MY_DATE_TIME_FORMATTER);

在哪里

MY_DATE_TIME_FORMATTER= new DateTimeFormatterBuilder()
            .parseCaseInsensitive()
            .append(ISO_LOCAL_DATE)
            .appendLiteral('T')
            .append(ISO_LOCAL_TIME)
            .appendLiteral('Z')
            .appendOffset("+HH:mm", "+0000")
            .toFormatter();

我得到以下异常

java.time.format.DateTimeParseException:无法在索引 19 处解析文本“2019-11-26T19:30:00Z”

你能告诉我我在这里做错了什么吗?

【问题讨论】:

  • 感谢@Jens 不幸的是我无法更改我的输入字符串。我只是以硬编码字符串为例。按原样输入字符串,我还能解析它吗?
  • 你能说出你的预期输出是什么
  • 你确定吗?我得到java.time.format.DateTimeParseException: Text '2019-11-26T19:30:00Z' could not be parsed at index 20,注意:20 而不是 19。

标签: java java-8 java-time zoneddatetime


【解决方案1】:

Instant.parse

不需要格式化模式。

Instant.parse( "2019-11-26T19:30:00Z" ) 

您的输入格式符合ISO 8601 标准。该特定格式的末尾有一个Z。该字母表示UTCoffset 时分秒为零),发音为“Zulu”。

java.time 中的 Instant 类表示 UTC 中的时刻,始终是 UTC。

对该输入使用ZonedDateTime 类不是最合适的。我们有:

  • Instant 表示始终采用 UTC 的值。
  • OffsetDateTime 适用于仅知道与 UTC 的偏移量但不知道时区的时刻。当您需要更大的灵活性(例如生成各种格式的字符串)时,也可以将此类用于 UTC 值。 `instant.atOffset(
  • ZonedDateTime 用于时区中的值。时区是特定地区的人们使用的偏移量的过去、现在和未来变化的历史。

要查看调整到特定区域(时区)的人们使用的偏移量的同一时刻,请应用 ZoneId 以获取 ZonedDateTime 对象。

Instant instant = Instant.parse( "2019-11-26T19:30:00Z" ) ;     // Default format for parsing a moment in UTC.
ZoneId z = ZoneId.of( "America/Edmonton" ) ;                    // A time zone is a history of past, present, and future changes to the offset used by the people of a particular region.
ZonedDateTime zdt = instant.atZone( z ) ;                       // Same moment, same point on the timeline, different wall-clock time.

看到这个code run live at IdeOne.com

instant.toString(): 2019-11-26T19:30:00Z

zdt.toString(): 2019-11-26T12:30-07:00[美国/埃德蒙顿]

【讨论】:

    【解决方案2】:

    添加

    .appendZoneOrOffsetId()
    

    而不是这两行:

    .appendLiteral('Z')
    .appendOffset("+HH:mm", "+0000")
    

    整个构建器示例:

    MY_DATE_TIME_FORMATTER= new DateTimeFormatterBuilder()
            .parseCaseInsensitive()
            .append(ISO_LOCAL_DATE)
            .appendLiteral('T')
            .append(ISO_LOCAL_TIME)
            .appendZoneOrOffsetId()
            .toFormatter();
    

    附:在您的具体情况下,我宁愿使用标准 ISO 格式化程序(如 Hristo 所述):

    ZonedDateTime z = ZonedDateTime.parse("2019-11-26T19:30:00Z", DateTimeFormatter.ISO_ZONED_DATE_TIME);
    

    此外,即使没有显式格式化程序,ZonedDateTime::parse 方法也可以工作。因为它是默认使用的:

    ZonedDateTime z = ZonedDateTime.parse("2019-11-26T19:30:00Z");
    

    【讨论】:

      【解决方案3】:

      使用内置的 ISO 分区时间格式化程序

          ZonedDateTime.parse("2019-11-26T19:30:00Z", DateTimeFormatter.ISO_ZONED_DATE_TIME);
      

      【讨论】:

        猜你喜欢
        • 2021-06-29
        • 2021-02-27
        • 1970-01-01
        • 1970-01-01
        • 2014-01-29
        • 1970-01-01
        • 2023-03-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多