JDK-8154050 : java.time.format.DateTimeFormatter can't parse localized zone-offset
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.time
  • Affected Version: 8,9
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2016-04-08
  • Updated: 2016-04-28
  • Resolved: 2016-04-21
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 9
9 b116Fixed
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.8.0_77"
Java(TM) SE Runtime Environment (build 1.8.0_77-b03)
Java HotSpot(TM) 64-Bit Server VM (build 25.77-b03, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Mac OS 10.11.1

A DESCRIPTION OF THE PROBLEM :
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)

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
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)

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Successful parse
ACTUAL -
StringIndexOutOfBoundsException

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

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
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)
---------- END SOURCE ----------


Comments
http://mail.openjdk.java.net/pipermail/core-libs-dev/2016-April/040150.html
13-04-2016

Issue in LocalizedOffsetIdPrinterParser.parse method
12-04-2016

Attached test case executed on: JDK 8 - Fail JDK 8u77 - Fail JDK 9EA - Fail The following exception is thrown in each case: Exception in thread "main" java.time.format.DateTimeParseException: Text '2016-04-12T07:53:19.9 GMT' could not be parsed: String index out of range: 25 at java.time.format.DateTimeFormatter.createError(java.base@9-ea/DateTimeFormatter.java:1932) at java.time.format.DateTimeFormatter.parse(java.base@9-ea/DateTimeFormatter.java:1793) at JI9034761.main(JI9034761.java:13) Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: 25 at java.lang.StringLatin1.charAt(java.base@9-ea/StringLatin1.java:45) at java.lang.String.charAt(java.base@9-ea/String.java:701) at java.time.format.DateTimeFormatterBuilder$LocalizedOffsetIdPrinterParser.parse(java.base@9-ea/DateTimeFormatterBuilder.java:3553) at java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.parse(java.base@9-ea/DateTimeFormatterBuilder.java:2212) at java.time.format.DateTimeFormatter.parseUnresolved0(java.base@9-ea/DateTimeFormatter.java:2022) at java.time.format.DateTimeFormatter.parseResolved0(java.base@9-ea/DateTimeFormatter.java:1951) at java.time.format.DateTimeFormatter.parse(java.base@9-ea/DateTimeFormatter.java:1789) ... 1 more
12-04-2016