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

DateTimeFormatter predefined formatters should support short time zone offsets

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P3
    • None
    • None
    • core-libs
    • None

    Description

      The bug fix for
      JDK-8032051
      "ZonedDateTime" class "parse" method fails with short time zone offset ("+01")
      fixed DateTimeFormatter.ISO_ZONED_DATE_TIME, but it seems like other DateTimeFormatter predefined formatters have the same problem, and need a similar fix. That is, any formatter that accepts an offset "+01:00" should also accept "+01" unless there's an industry spec to the contrary.

      (as reported by a colleague)

      import java.time.format.DateTimeFormatter;
      import static java.time.format.DateTimeFormatter.ISO_DATE;
      import static java.time.format.DateTimeFormatter.ISO_DATE_TIME;
      import static java.time.format.DateTimeFormatter.ISO_OFFSET_DATE;
      import static java.time.format.DateTimeFormatter.ISO_OFFSET_DATE_TIME;
      import static java.time.format.DateTimeFormatter.ISO_OFFSET_TIME;
      import static java.time.format.DateTimeFormatter.ISO_TIME;
      import static java.time.format.DateTimeFormatter.ISO_ZONED_DATE_TIME;

      public class DateTimeParse {
          static void test(DateTimeFormatter formatter, String x) {
              try {
                  formatter.parse(x);
              } catch (Throwable t) { t.printStackTrace(); }
          }

          public static void main(String[] args) throws Throwable {
              // no exceptions
              test(ISO_ZONED_DATE_TIME, "2013-12-11T21:25:04.800842+01:00");
              test(ISO_ZONED_DATE_TIME, "2013-12-11T21:25:04.800842+01");
              test(ISO_OFFSET_DATE_TIME, "2013-12-11T21:25:04.800842+01:00");
              test(ISO_OFFSET_DATE_TIME, "2013-12-11T21:25:04.800842+01");

              // exceptions ...
              test(ISO_DATE_TIME, "2013-12-11T21:25:04.800842+01:00");
              test(ISO_DATE_TIME, "2013-12-11T21:25:04.800842+01");
              test(ISO_OFFSET_DATE, "2013-12-11+01:00");
              test(ISO_OFFSET_DATE, "2013-12-11+01");
              test(ISO_DATE, "2013-12-11+01:00");
              test(ISO_DATE, "2013-12-11+01");
              test(ISO_OFFSET_TIME, "21:25:04.800842+01:00");
              test(ISO_OFFSET_TIME, "21:25:04.800842+01");
              test(ISO_TIME, "21:25:04.800842+01:00");
              test(ISO_TIME, "21:25:04.800842+01");
          }
      }


      =>

      java.time.format.DateTimeParseException: Text '2013-12-11T21:25:04.800842+01' could not be parsed, unparsed text found at index 26
      at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2049)
      at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1874)
      at DateTimeParse.test(DateTimeParse.java:13)
      at DateTimeParse.main(DateTimeParse.java:26)
      java.time.format.DateTimeParseException: Text '2013-12-11+01' could not be parsed at index 10
      at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2046)
      at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1874)
      at DateTimeParse.test(DateTimeParse.java:13)
      at DateTimeParse.main(DateTimeParse.java:28)
      java.time.format.DateTimeParseException: Text '2013-12-11+01' could not be parsed, unparsed text found at index 10
      at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2049)
      at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1874)
      at DateTimeParse.test(DateTimeParse.java:13)
      at DateTimeParse.main(DateTimeParse.java:30)
      java.time.format.DateTimeParseException: Text '21:25:04.800842+01' could not be parsed at index 15
      at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2046)
      at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1874)
      at DateTimeParse.test(DateTimeParse.java:13)
      at DateTimeParse.main(DateTimeParse.java:32)
      java.time.format.DateTimeParseException: Text '21:25:04.800842+01' could not be parsed, unparsed text found at index 15
      at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2049)
      at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1874)
      at DateTimeParse.test(DateTimeParse.java:13)
      at DateTimeParse.main(DateTimeParse.java:34)

      Attachments

        Issue Links

          Activity

            People

              ntv Nadeesh Tv
              martin Martin Buchholz
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated: