JDK-8033662 : java.time.format.DateTimeFormatter doesn't set zone when parsing
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.time
  • Affected Version: 8
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_8
  • CPU: x86
  • Submitted: 2014-02-04
  • Updated: 2014-12-09
  • Resolved: 2014-03-24
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.
JDK 8 JDK 9
8u20Fixed 9 b08Fixed
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.8.0-ea"
Java(TM) SE Runtime Environment (build 1.8.0-ea-b123)
Java HotSpot(TM) 64-Bit Server VM (build 25.0-b65, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 6.3.9600]

A DESCRIPTION OF THE PROBLEM :
The javadoc of the method java.time.format.DateTimeFormatter::withZone says: "If no zone has been parsed, then this override zone will be included in the result of the parse where it can be used to build instants and date-times." However, the implementation doesn't obey this.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss").withZone(ZoneOffset.UTC);
TemporalAccessor acc = fmt.parse("01/08/2012 00:00:01");
ZonedDateTime zdt = ZonedDateTime.from(acc);

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The code runs without throwing an exception.
ACTUAL -
An exception is thrown.


ERROR MESSAGES/STACK TRACES THAT OCCUR :
Exception in thread "main" java.time.DateTimeException: Unable to obtain ZonedDateTime from TemporalAccessor: {},ISO resolved to 2012-08-01T00:00:01 of type java.time.format.Parsed
	at java.time.ZonedDateTime.from(ZonedDateTime.java:564)
	at Jsr310Bug.main(Jsr310Bug.java:11)
Caused by: java.time.DateTimeException: Unable to obtain ZoneId from TemporalAccessor: {},ISO resolved to 2012-08-01T00:00:01 of type java.time.format.Parsed
	at java.time.ZoneId.from(ZoneId.java:466)
	at java.time.ZonedDateTime.from(ZonedDateTime.java:552)
	... 1 more
Java Result: 1

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;

public class Jsr310Bug {
    
    public static void main(String[] args) {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss").withZone(ZoneOffset.UTC);
        TemporalAccessor acc = fmt.parse("01/08/2012 00:00:01");
        ZonedDateTime zdt = ZonedDateTime.from(acc);
    }
    
}

---------- END SOURCE ----------


Comments
Attaching patch for IP reasons
12-03-2014

This is an unfortunate double bug, which prevents a key piece of functionality from working. The second part of the bug was a NullPointerException that was being hidden by a test that expected an exception. (This occurred because the zone was wrongly assumed to be non-null when interpretting the result of TemporalField.resolve) Proposed patch: https://gist.github.com/jodastephen/9505761
12-03-2014