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

DateTimeFormatter.ISO_INSTANT should handle offsets

XMLWordPrintable

    • b15
    • generic
    • generic

      FULL PRODUCT VERSION :


      A DESCRIPTION OF THE PROBLEM :
      Both "2016-09-12T16:45:51+00:00" and "2016-09-12T16:45:51Z" are valid ISO-8601 representations of java.time.Instant. However, Instant.parse() will only parse the latter, but not the former.

      The problem actually lies in java.time.format.DateTimeFormatter.ISO_INSTANT. According to the documentation (http://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_INSTANT), it inherits from DateTimeFormatter.ISO_OFFSET_DATE_TIME. However, the documentation is incorrect and that is not the way it was implemented.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Instant instant = Instant.parse("2016-09-12T16:45:51+00:00");

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      No exceptions thrown, with instant properly initialized.
      ACTUAL -
      java.time.format.DateTimeParseException: Text '2016-09-12T16:45:51+00:00' could not be parsed at index 19

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      java.time.format.DateTimeParseException: Text '2016-09-12T16:45:51+00:00' could not be parsed at index 19

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.time.Instant;

      import org.junit.Assert;
      import org.junit.Test;

      public class MyTest {

          @Test
          public void test() {
              Instant instant1 = Instant.parse("2016-09-12T16:45:51+00:00");
              Instant instant2 = Instant.parse("2016-09-12T16:45:51Z");
              Assert.assertEquals(instant2, instant1);
          }

      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      DateTimeFormatter.ISO_OFFSET_DATE_TIME can be used directly to create the Instant.

      This is not always practical for all use cases (such as where Instant is being unmarshalled using a third-party library).

        1. Capture.PNG
          Capture.PNG
          19 kB
        2. MyTest.java
          0.3 kB

            psonal Pallavi Sonal (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: