JDK-8033659 : parsing java.time.Instant without a zoneId may be broken
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.time
  • Affected Version: 8
  • Priority: P4
  • Status: Resolved
  • Resolution: Duplicate
  • OS: windows_8
  • CPU: x86
  • Submitted: 2014-02-04
  • Updated: 2014-04-02
  • Resolved: 2014-03-26
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.
Other
tbd_minorResolved
Related Reports
Duplicate :  
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));


Comments
dup of 8033662
26-03-2014

This can be resolved as a duplicate of JDK-8033662
26-03-2014

This would appear to be a bug at first glance. The purpose of withZone() is to allow a missing zone to be added into the format/parse.
05-02-2014