尽管直接从文档中复制,为什么 `GMT+8` 无法使用模式 `O` 进行解析?
Why does `GMT+8` fail to parse with pattern `O` despite being copied straight out of doc?
为什么以下无法 运行,日期时间字符串无法解析为 OffsetDateTime
?
String inputOdt = "2016-01-23T12:34:56 GMT+8";
DateTimeFormatter formatterOdt = DateTimeFormatter.ofPattern ( "yyyy-MM-dd'T'HH:mm:ss O" );
OffsetDateTime odt = OffsetDateTime.parse ( inputOdt , formatterOdt );
在 Mac OS X El Capitan 10.11.4.
上使用 Java(TM) SE Runtime Environment (build 1.8.0_92-b14)
产生错误:
Exception in thread "main" java.time.format.DateTimeParseException: Text '2016-01-23T12:34:56 GMT+8' could not be parsed: String index out of range: 25
offset-from-UTC string GMT+8
is copied-pasted from the example in the class documentation for DateTimeFormatter
。引用:
Offset O: This formats the localized offset based on the number of pattern letters. One letter outputs the short form of the localized offset, which is localized offset text, such as 'GMT', with hour without leading zero, optional 2-digit minute and second if non-zero, and colon, for example 'GMT+8'.
字符串的其余部分成功解析为 LocalDateTime
。所以问题似乎确实是与 UTC 的偏移量部分。
String inputLdt = "2016-01-23T12:34:56";
DateTimeFormatter formatterLdt = DateTimeFormatter.ofPattern ( "yyyy-MM-dd'T'HH:mm:ss" );
LocalDateTime ldt = LocalDateTime.parse ( inputLdt , formatterLdt );
System.out.println ( "" );
System.out.println ( "inputLdt: " + inputLdt );
System.out.println ( "ldt: " + ldt );
inputLdt: 2016-01-23T12:34:56
ldt: 2016-01-23T12:34:56
解决方法
部分解决方法是在输入字符串和格式化模式中添加尾随 SPACE。所以这有效。
String input = "Sat May 02 2015 00:00:00 GMT+08 "; // Trailing space.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern ( "EEE MMM dd yyyy HH:mm:ss O " ); // Trailing space.
OffsetDateTime odt = OffsetDateTime.parse ( input , formatter ); // SUCCEEDS
但是在没有冒号的情况下添加分钟被记录为使用单个 O
但它失败了。这种尾随 SPACE 的解决方法在这种情况下没有帮助。请注意此示例中的 GMT+0800
与上面直接看到的 GMT+08
,其中此示例失败但上面的示例成功。
String input = "Sat May 02 2015 00:00:00 GMT+0800 "; // Minutes in the offset, and trailing space.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern ( "EEE MMM dd yyyy HH:mm:ss O " ); // Trailing space.
OffsetDateTime odt = OffsetDateTime.parse ( input , formatter ); // FAILS
似乎是 Java 中的错误。见 https://bugs.openjdk.java.net/browse/JDK-8154050:
java.time.format.DateTimeFormatter can't parse localized zone-offset
The DateTimeFormatter fails to parse its own output for format strings containing "O". The following code throws a StringIndexOutOfBoundsException on the final line.
import java.time.ZoneOffset
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
DateTimeFormatter formatter = DateTimeFormatter
.ofPattern("yyyy-MM-dd'T'HH:mm:ss.S O")
.withLocale(Locale.ENGLISH)
String date = formatter.format(ZonedDateTime.now(ZoneOffset.UTC));
formatter.parse(date)
ERROR MESSAGES/STACK TRACES THAT OCCUR :
java.time.format.DateTimeParseException: Text '2016-04-08T10:49:52.7 GMT' could not be parsed: String index out of range: 25
并在评论中:
Attached test case executed on:
JDK 8 - Fail
JDK 8u77 - Fail
JDK 9EA - Fail
似乎已在 Java 9 build b116 中修复。
我也遇到了同样的问题
我的字符串类似于“28.04.2010 09:39:33 UTC+2”。
我必须在偏移量中添加一个 0 ("UTC+02")。得到它解析。
作为我使用的模式:
public final static String INPUT_PATTERN_DD_MM_YYYY_HH_mm_ss_zzz = "dd.MM.yyyy HH:mm:ss zzz";
由于偏移量可能为零("UTC" 或 "GMT" 没有数字),我使用的是 DateTimeFormatterBuilder:
DateTimeFormatter formatter = new DateTimeFormatterBuilder().parseCaseInsensitive().append(df).optionalStart()
.appendPattern("X").optionalEnd().toFormatter();
其中 "X" 是区域偏移...
不过我还需要:
ZoneId id = ZoneOffset.ofHours(Integer.valueOf(offset));
zonedDateTime = zonedDateTime.withZoneSameInstant(id);
zonedDateTime = zonedDateTime.minusHours(Integer.valueOf(offset));
真的很尴尬...:-(
希望java9能做好工作
为什么以下无法 运行,日期时间字符串无法解析为 OffsetDateTime
?
String inputOdt = "2016-01-23T12:34:56 GMT+8";
DateTimeFormatter formatterOdt = DateTimeFormatter.ofPattern ( "yyyy-MM-dd'T'HH:mm:ss O" );
OffsetDateTime odt = OffsetDateTime.parse ( inputOdt , formatterOdt );
在 Mac OS X El Capitan 10.11.4.
上使用Java(TM) SE Runtime Environment (build 1.8.0_92-b14)
产生错误:
Exception in thread "main" java.time.format.DateTimeParseException: Text '2016-01-23T12:34:56 GMT+8' could not be parsed: String index out of range: 25
offset-from-UTC string GMT+8
is copied-pasted from the example in the class documentation for DateTimeFormatter
。引用:
Offset O: This formats the localized offset based on the number of pattern letters. One letter outputs the short form of the localized offset, which is localized offset text, such as 'GMT', with hour without leading zero, optional 2-digit minute and second if non-zero, and colon, for example 'GMT+8'.
字符串的其余部分成功解析为 LocalDateTime
。所以问题似乎确实是与 UTC 的偏移量部分。
String inputLdt = "2016-01-23T12:34:56";
DateTimeFormatter formatterLdt = DateTimeFormatter.ofPattern ( "yyyy-MM-dd'T'HH:mm:ss" );
LocalDateTime ldt = LocalDateTime.parse ( inputLdt , formatterLdt );
System.out.println ( "" );
System.out.println ( "inputLdt: " + inputLdt );
System.out.println ( "ldt: " + ldt );
inputLdt: 2016-01-23T12:34:56
ldt: 2016-01-23T12:34:56
解决方法
部分解决方法是在输入字符串和格式化模式中添加尾随 SPACE。所以这有效。
String input = "Sat May 02 2015 00:00:00 GMT+08 "; // Trailing space.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern ( "EEE MMM dd yyyy HH:mm:ss O " ); // Trailing space.
OffsetDateTime odt = OffsetDateTime.parse ( input , formatter ); // SUCCEEDS
但是在没有冒号的情况下添加分钟被记录为使用单个 O
但它失败了。这种尾随 SPACE 的解决方法在这种情况下没有帮助。请注意此示例中的 GMT+0800
与上面直接看到的 GMT+08
,其中此示例失败但上面的示例成功。
String input = "Sat May 02 2015 00:00:00 GMT+0800 "; // Minutes in the offset, and trailing space.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern ( "EEE MMM dd yyyy HH:mm:ss O " ); // Trailing space.
OffsetDateTime odt = OffsetDateTime.parse ( input , formatter ); // FAILS
似乎是 Java 中的错误。见 https://bugs.openjdk.java.net/browse/JDK-8154050:
java.time.format.DateTimeFormatter can't parse localized zone-offset
The DateTimeFormatter fails to parse its own output for format strings containing "O". The following code throws a StringIndexOutOfBoundsException on the final line.
import java.time.ZoneOffset import java.time.ZonedDateTime import java.time.format.DateTimeFormatter DateTimeFormatter formatter = DateTimeFormatter .ofPattern("yyyy-MM-dd'T'HH:mm:ss.S O") .withLocale(Locale.ENGLISH) String date = formatter.format(ZonedDateTime.now(ZoneOffset.UTC)); formatter.parse(date)
ERROR MESSAGES/STACK TRACES THAT OCCUR :
java.time.format.DateTimeParseException: Text '2016-04-08T10:49:52.7 GMT' could not be parsed: String index out of range: 25
并在评论中:
Attached test case executed on:
JDK 8 - Fail
JDK 8u77 - Fail
JDK 9EA - Fail
似乎已在 Java 9 build b116 中修复。
我也遇到了同样的问题
我的字符串类似于“28.04.2010 09:39:33 UTC+2”。
我必须在偏移量中添加一个 0 ("UTC+02")。得到它解析。 作为我使用的模式:
public final static String INPUT_PATTERN_DD_MM_YYYY_HH_mm_ss_zzz = "dd.MM.yyyy HH:mm:ss zzz";
由于偏移量可能为零("UTC" 或 "GMT" 没有数字),我使用的是 DateTimeFormatterBuilder:
DateTimeFormatter formatter = new DateTimeFormatterBuilder().parseCaseInsensitive().append(df).optionalStart()
.appendPattern("X").optionalEnd().toFormatter();
其中 "X" 是区域偏移...
不过我还需要:
ZoneId id = ZoneOffset.ofHours(Integer.valueOf(offset));
zonedDateTime = zonedDateTime.withZoneSameInstant(id);
zonedDateTime = zonedDateTime.minusHours(Integer.valueOf(offset));
真的很尴尬...:-(
希望java9能做好工作