gpt4 book ai didi

datetime - Java 8 LocalDateTime ZonedDateTime 无法解析带时区的日期

转载 作者:行者123 更新时间:2023-12-04 10:48:08 26 4
gpt4 key购买 nike

我正在尝试使用 Java 8 新日期模式而不是 Joda,但我遇到了以下问题:

两个都

ZonedDateTime.parse("02/05/16 11:51.12.083 +04:30", DateTimeFormatter.ofPattern("dd/MM/yy HH:mm.ss.SSS Z"))


LocalDateTime.parse("02/05/16 11:51.12.083 +04:30", DateTimeFormatter.ofPattern("dd/MM/yy HH:mm.ss.SSS Z"))

抛出“java.time.format.DateTimeParseException”异常。
尽管
org.joda.time.DateTime.parse("02/05/16 11:51.12.083 +04:30", DateTimeFormat.forPattern("dd/MM/yy HH:mm.ss.SSS Z"))

工作正常。

异常的原因是:

java.time.format.DateTimeParseException: Text '02/05/16 11:51.12.083 +04:30' could not be parsed at index 22



难道我做错了什么?

最佳答案

如果您阅读 the javadoc of DateTimeFormatter ,您将找到详细说明如何使用 Z 的部分。抵消(强调我的):

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.



所以使用 5 Z s 将按预期工作:
ZonedDateTime.parse("02/05/16 11:51.12.083 +04:30",
DateTimeFormatter.ofPattern("dd/MM/yy HH:mm.ss.SSS ZZZZZ"));

请注意,您可以通过以下方式获得类似的结果:
  • z
  • zz
  • zzz
  • zzzz
  • xxx
  • XXX
  • xxxxx
  • XXXXX
  • 关于datetime - Java 8 LocalDateTime ZonedDateTime 无法解析带时区的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37253407/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com