我有日期字符串“2015-01-12T13:00:00.000+02:00”。查看 JavaDoc 我看到以下内容:
Run Code Online (Sandbox Code Playgroud)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");
Run Code Online (Sandbox Code Playgroud)
但这样我就得到了解析错误。
如果我使用小写“z”,它会起作用:
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.000z")
Run Code Online (Sandbox Code Playgroud)
有谁知道发生了什么事吗?
代码:
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);
Run Code Online (Sandbox Code Playgroud)
异常堆栈跟踪:
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)
Run Code Online (Sandbox Code Playgroud)