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

java.time.format.DateTimeFormatter doesn't set zone when parsing

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P3
    • Resolution: Fixed
    • Affects Version/s: 8
    • Fix Version/s: 9
    • Component/s: core-libs
    • Labels:
    • Subcomponent:
    • Resolved In Build:
      b08
    • CPU:
      x86
    • OS:
      windows_8

      Backports

        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 ----------

          Attachments

            Issue Links

              Activity

                People

                Assignee:
                rriggs Roger Riggs
                Reporter:
                webbuggrp Webbug Group
                Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                  Dates

                  Created:
                  Updated:
                  Resolved: