[英]Java 8 DateTimeFormatter: confusion with 'z' and 'Z ' pattern letters
I have the date string "2015-01-12T13:00:00.000+02:00".我有日期字符串“2015-01-12T13:00:00.000+02:00”。 Looking at the JavaDoc I see the following:
查看 JavaDoc 我看到以下内容:
z time-zone name zone-name Pacific Standard Time; PST Z zone-offset offset-Z +0000; -0800; -08:00;
So I would suspect that to parse it I would have to use upper-case 'Z' because I have the Zone format given in +02:00:所以我怀疑要解析它,我必须使用大写的“Z”,因为我在 +02:00 中给出了 Zone 格式:
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.000Z");
But with that I get a parse error.但是我得到了一个解析错误。
If I use lower-case 'z' it works:如果我使用小写的“z”,它会起作用:
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.000z")
Does anyone know what is going on?有谁知道发生了什么?
CODE:代码:
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);
Exception stack trace:异常堆栈跟踪:
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)
I would believe it's an error in the Javadoc and this part is a mistake我相信这是 Javadoc 中的错误,这部分是错误的
Symbol Meaning Presentation Examples<br>
------ ------- ------------ -------
Z zone-offset offset-Z +0000; -0800; -08:00;
because if you read further you find the explanation of the offset Z
因为如果你进一步阅读,你会发现偏移量
Z
的解释
Offset Z : This formats the offset based on the number of pattern letters.
Offset 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.
当偏移量为零时,输出将为“+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.
如果偏移为零,则输出将是相应的本地化偏移文本。 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。
Which complies RFC 822 .这符合RFC 822 。
For me the pattern should behave same as for SimpleDateFormat对我来说,模式的行为应该与SimpleDateFormat相同
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.