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

parsing java.time.Instant without a zoneId may be broken

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P4
    • Resolution: Duplicate
    • Affects Version/s: 8
    • Fix Version/s: tbd_minor
    • Component/s: core-libs
    • Labels:
    • Subcomponent:
    • CPU:
      x86
    • OS:
      windows_8

      Description

      FULL PRODUCT VERSION :
      java version "1.8.0-ea"
      Java(TM) SE Runtime Environment (build 1.8.0-ea-b124)
      Java HotSpot(TM) 64-Bit Server VM (build 25.0-b66, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Windows 7 64 bits, Windows 8.1 64 bits

      A DESCRIPTION OF THE PROBLEM :
      Hello,

      I'm trying to use the new java.time api that comes with Java 8.

      I've a String "01/08/2012_00:00:01", the time ref. is UTC, and I want to convert it to an 'Instant'.

      I tried (The code seems compliant with the JavaDoc.):

      DateTimeFormatter FORMAT_DT = DateTimeFormatter.ofPattern("dd/MM/yyyy_HH:mm:ss").withZone(ZoneOffset.UTC);
      Instant instant = Instant.from(FORMAT_DT.parse("01/08/2012_00:00:01"));

      But it fails on the second line with following error:

      java.time.DateTimeException: Unable to obtain Instant from TemporalAccessor: {},ISO resolved to 2012-08-01T00:00:01 of type java.time.format.Parsed
      at java.time.Instant.from(Unknown Source)



      Note: Using first a LocalDateTime instead of an Instant works:

      LocalDateTime ldt = LocalDateTime.from(FORMAT_DT.parse("01/08/2012_00:00:01"));
      Instant instant = Instant.from(ldt.atZone(ZoneOffset.UTC));

      But it seems strange to have to go through a LocalDateTime to convert a String to an Instant ...


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run following code:
      DateTimeFormatter FORMAT_DT = DateTimeFormatter.ofPattern("dd/MM/yyyy_HH:mm:ss").withZone(ZoneOffset.UTC);
      Instant instant = Instant.from(FORMAT_DT.parse("01/08/2012_00:00:01"));

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      'instant' should contain a valid value
      ACTUAL -
      An java.time.DateTimeException is raised.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      java.time.DateTimeException: Unable to obtain Instant from TemporalAccessor: {},ISO resolved to 2012-08-01T00:00:01 of type java.time.format.Parsed
      at java.time.Instant.from(Unknown Source)

      REPRODUCIBILITY :
      This bug can be reproduced always.

      CUSTOMER SUBMITTED WORKAROUND :
      DateTimeFormatter FORMAT_DT = DateTimeFormatter.ofPattern("dd/MM/yyyy_HH:mm:ss").withZone(ZoneOffset.UTC);
      LocalDateTime ldt = LocalDateTime.from(FORMAT_DT.parse("01/08/2012_00:00:01"));
      Instant instant = Instant.from(ldt.atZone(ZoneOffset.UTC));

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              sherman Xueming Shen
              Reporter:
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: