简体   繁体   English

Java 8 DateTimeFormatter:与“z”和“Z”模式字母混淆

[英]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.

相关问题 Java DateTimeFormatter 在时区为 +0000 时添加 Z - Java DateTimeFormatter adding a Z when timezone is +0000 DateTimeFormatter - java.lang.IllegalArgumentException:模式字母太多:a - DateTimeFormatter - java.lang.IllegalArgumentException: Too many pattern letters: a 使用Java流将字母从A打印到Z. - Printing the letters from A to Z using a Java stream Java DateTimeFormatter分数模式? - Java DateTimeFormatter fraction pattern? 解决 Java Checkstyle 错误:名称 &#39;logger&#39; 必须匹配模式 &#39;^[AZ][A-Z0-9]*(_[A-Z0-9]+)*$&#39; - Resolve Java Checkstyle Error: Name 'logger' must match pattern '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$' Java中文本文件中字母(az)的相对频率计数 - Relative Frequency Count of letters (a-z) from a text file in Java Java-如何在不使用!@#$%^&*()的情况下从0打印到z?&gt; &lt;“:{} |字母,仅包括字母和数字 - Java - How to print from 0 to z without !@#$%^&*()?><":{}| letters, only alphabet and numbers 为什么Joda DateTimeFormatter无法解析时区名称('z') - Why Joda DateTimeFormatter cannot parse timezone names ('z') 使用 DateTimeFormatter 时出现“z”(时区名称)上的 DateTimeParseException - DateTimeParseException on 'z' (time-zone name) when using DateTimeFormatter Java 代码约定:必须匹配模式 &#39;^[az][a-zA-Z0-9]*$&#39; - Java Code Conventions: must match pattern '^[a-z][a-zA-Z0-9]*$'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM