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

DateTimeFormatter parsing ignores withZone()

XMLWordPrintable

    • x86_64
    • windows_10

      A DESCRIPTION OF THE PROBLEM :
      When parsing ZonedDateTime and format doesn't contain a time part, method parse fails with exception 'Unable to obtain ZonedDateTime from TemporalAccessor'

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the submitted source code.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      2018-09-19T00:00Z
      ACTUAL -
      java.time.format.DateTimeParseException: Text '2018-09-19' could not be parsed: Unable to obtain ZonedDateTime from TemporalAccessor: {},ISO,Z resolved to 2018-09-19 of type
      java.time.format.Parsed
              at java.time.format.DateTimeFormatter.createError(Unknown Source)
              at java.time.format.DateTimeFormatter.parse(Unknown Source)
              at java.time.ZonedDateTime.parse(Unknown Source)
              at ZonedDateParsingBug.main(ZonedDateParsingBug.java:14)
      Caused by: java.time.DateTimeException: Unable to obtain ZonedDateTime from TemporalAccessor: {},ISO,Z resolved to 2018-09-19 of type java.time.format.Parsed
              at java.time.ZonedDateTime.from(Unknown Source)
              at java.time.format.Parsed.query(Unknown Source)
              ... 3 more
      Caused by: java.time.DateTimeException: Unable to obtain LocalTime from TemporalAccessor: {},ISO,Z resolved to 2018-09-19 of type java.time.format.Parsed
              at java.time.LocalTime.from(Unknown Source)
              ... 5 more

      ---------- BEGIN SOURCE ----------
      import java.time.ZoneOffset;
      import java.time.ZonedDateTime;
      import java.time.format.DateTimeFormatter;
      import java.util.Locale;

      public class ZonedDateParsingBug {

          public static void main(String args[]) {
              int retval = 1;
              try {
                  DateTimeFormatter inputDateTimeFormatter =
                          DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.ROOT)
                          .withZone(ZoneOffset.UTC);
                  ZonedDateTime dateTime = ZonedDateTime.parse("2018-09-19", inputDateTimeFormatter);
                  System.out.printf("%s%n", dateTime);
                  retval = 0;
              } catch (Exception e) {
                  e.printStackTrace();
              }
              System.exit(retval);
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Catch exception, and compose ZonedDateTime object from LocalDateTime.

      FREQUENCY : always


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

              Created:
              Updated:
              Resolved: