Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8206980

DateTimeFormatter throws parsing a valid string depending on the locale

XMLWordPrintable

    • b12
    • 11
    • b23
    • generic
    • generic
    • Verified

        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


              naoto Naoto Sato
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              7 Start watching this issue

                Created:
                Updated:
                Resolved: