JDK-8206980 : DateTimeFormatter throws parsing a valid string depending on the locale
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.time
  • Affected Version: 11
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2018-07-09
  • Updated: 2018-07-23
  • Resolved: 2018-07-11
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 11 JDK 12
11 b23Fixed 12Fixed
Related Reports
Relates :  
Description
ADDITIONAL SYSTEM INFORMATION :
macOs High Sierra version 10.13.5

java 11-ea 2018-09-25
Java(TM) SE Runtime Environment 18.9 (build 11-ea+21)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11-ea+21, mixed mode)

A DESCRIPTION OF THE PROBLEM :
DateTimeFormatter.format������(TemporalAccessor temporal) throws a DateTimeParseException on encountering a 'Z' depending on the locale the DateTimeFormatter is constructed with. In this case the exception is thrown if Locale.CANADA is used but not if Locale.US is used. 

This appears to be a regression as I cannot reproduce the error in Java 8, 9, or 10

REGRESSION : Last worked in version 11

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the test case below (javac DateFormatBug.java; java -cp . DateFormatBug) and in Java 11 it throws an exception:

Exception in thread "main" java.time.format.DateTimeParseException: Text '1970.01.01 at 00:00:00 Z' could not be parsed, unparsed text found at index 23
	at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2049)
	at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1874)
	at DateFormatBug.main(DateFormatBug.java:18)


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The date time string should be parsed without error. If the locale Locale.US is used the exception isn't thrown. Java 10 does not throw the exception for any locale I tested. 
ACTUAL -
A DateTimeParseException is thrown.

Exception in thread "main" java.time.format.DateTimeParseException: Text '1970.01.01 at 00:00:00 Z' could not be parsed, unparsed text found at index 23
	at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2049)
	at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1874)
	at DateFormatBug.main(DateFormatBug.java:18)


---------- BEGIN SOURCE ----------
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.TemporalAccessor;
import java.util.Locale;

public class DateFormatBug {

    public static void main(String [] args) {
        DateTimeFormatter formatter = new DateTimeFormatterBuilder()
                .parseLenient()
                .appendPattern("yyyy.MM.dd 'at' HH:mm:ss z")
                .toFormatter(Locale.CANADA);

        String datetime = formatter.format(ZonedDateTime.ofInstant(Instant.ofEpochSecond(0), ZoneOffset.UTC));
        TemporalAccessor parsed = formatter.parse(datetime);
    }
}
---------- END SOURCE ----------

FREQUENCY : always



Comments
This is a regression caused by the fix to JDK-8181157, in which English time zone names in CLDRTimeZoneNameProvider are pre-filled (no "empty" zone names). The logic to determine the path was incorrectly omitting English locales other than US, such as Locale.CANADA. Thus other non-US English locales such as Locale.UK can also trigger this issue.
11-07-2018

To reproduce the issue run the attached test case. JDK 10.0.1 - Pass JDK 11-ea+b11 - Pass JDK 11-ea+12 - Fail JDK 11-ea+18 - Fail Exception in thread "main" java.time.format.DateTimeParseException: Text '1970.01.01 at 00:00:00 Z' could not be parsed, unparsed text found at index 23 at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2049) at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1874) at JI9055975.main(JI9055975.java:18)
10-07-2018