gpt4 book ai didi

Java 8 日期时间格式化程序 : confusion with 'z' and 'Z ' pattern letters

转载 作者:行者123 更新时间:2023-11-30 10:46:55 25 4
gpt4 key购买 nike

我有日期字符串“2015-01-12T13:00:00.000+02:00”。查看 JavaDoc,我看到以下内容:

z       time-zone name              zone-name         Pacific Standard Time; PST
Z zone-offset offset-Z +0000; -0800; -08:00;


所以我怀疑要解析它,我必须使用大写的“Z”,因为我有 +02:00 中给出的区域格式:

DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.000Z");

但是我得到了一个解析错误。

如果我使用小写字母“z”,它会起作用:

DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.000z")


有谁知道这是怎么回事吗?


代码:

DateTimeFormatter changetimeParser_Z = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.000Z");
DateTimeFormatter changetimeParser_z = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.000z");

String time = "2015-01-12T13:00:00.000+02:00";

ZonedDateTime time1 = ZonedDateTime.parse(time, changetimeParser_z);
System.out.println(time1);
ZonedDateTime time2 = ZonedDateTime.parse(time, changetimeParser_Z);

System.out.println(time2);


异常堆栈跟踪:

2015-01-12T13:00+02:00
java.time.format.DateTimeParseException: Text '2015-01-12T13:00:00.000+02:00' could not be parsed at index 23
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.time.ZonedDateTime.parse(ZonedDateTime.java:597)

最佳答案

我认为这是 Javadoc 中的一个错误,这部分是一个错误

Symbol  Meaning                     Presentation      Examples<br>
------ ------- ------------ -------
Z zone-offset offset-Z +0000; -0800; -08:00;

因为如果你进一步阅读,你会发现偏移量 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.

符合 RFC 822 .

对我来说,模式的行为应该与 SimpleDateFormat 相同

关于Java 8 日期时间格式化程序 : confusion with 'z' and 'Z ' pattern letters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36325982/

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