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

java.time.format.FormatStyle.LONG or FULL causes unchecked exception

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P4
    • Resolution: Fixed
    • Affects Version/s: 8u45
    • Fix Version/s: 9
    • Component/s: core-libs
    • Labels:
    • Subcomponent:
    • Resolved In Build:
      b112
    • CPU:
      x86
    • OS:
      os_x

      Description

      A DESCRIPTION OF THE PROBLEM :
      I have filed this as a documentation error as after discussions I don't think it is a software bug. Rather, that the documentation is ambiguous about when a FormatStyle.LONG or FULL can be passed as an argument to a LocalDateTime object formatter.

      The following throws an unchecked exception (shown after) on the formatter assignment line:
      -----------------------------
      ...
      LocalDateTime rightNow = LocalDateTime.now();
      ...
      DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
      System.out.println(dateTimeFormatter.format(rightNow));
      -----------------------------
      Exception:
      -----------------------------
      Exception in thread "main" java.time.DateTimeException: Unable to extract value: class java.time.LocalDateTime
          at java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:282)
          at java.time.format.DateTimeFormatterBuilder$ZoneTextPrinterParser.format(DateTimeFormatterBuilder.java:3685)
          at java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.format(DateTimeFormatterBuilder.java:2182)
          at java.time.format.DateTimeFormatterBuilder$LocalizedPrinterParser.format(DateTimeFormatterBuilder.java:4350)
          at java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.format(DateTimeFormatterBuilder.java:2182)
          at java.time.format.DateTimeFormatter.formatTo(DateTimeFormatter.java:1744)
          at java.time.format.DateTimeFormatter.format(DateTimeFormatter.java:1718)
          at TimeTest.main(TimeTest.java:33)
      -----------------------------
      However, the following two variants execute without problem:
      -----------------------------
      DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG, FormatStyle.SHORT);
      System.out.println("now: " + dateTimeFormatter.format(rightNow));
      -----------------------------
      DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
      System.out.println("now: " + dateTimeFormatter.format(rightNow));
      -----------------------------

      Even using FormatStyle.FULL causes the same exception although the API documentation clearly shows a time as part of this output. Although LONG does not show this, I would still expect it to provide the time as part of the format. Nowhere in the documentation does it say that LONG and FULL cannot be used with a time.

      Output is as expected if the following is executed:
      -----------------------------
      LocalDateTime rightNow = LocalDateTime.now();
      ZonedDateTime nowWithTimeZone = ZonedDateTime.of(rightNow, ZoneId.of("America/New_York"));
      DateTimeFormatter dateTimeFormatter3 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL);
      System.out.println("now: " + dateTimeFormatter3.format(nowWithTimeZone));

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      This is difficult to say because I am only assuming there is no bug in the software. But I would have expected the documentation to detail any use cases where thee constants cannot be applied.
      ACTUAL -
      Enumeration of the style of a localized date, time or date-time formatter.
      These styles are used when obtaining a date-time style from configuration. See DateTimeFormatter and DateTimeFormatterBuilder for usage.
      ...
      Enum Constant Detail

      FULL
      public static final FormatStyle FULL
      Full text style, with the most detail. For example, the format might be 'Tuesday, April 12, 1952 AD' or '3:30:42pm PST'.
      LONG
      public static final FormatStyle LONG
      Long text style, with lots of detail. For example, the format might be 'January 12, 1952'.

      URL OF FAULTY DOCUMENTATION :
      https://docs.oracle.com/javase/8/docs/api/java/time/format/FormatStyle.html

        Issue Links

          Activity

          Hide
          rriggs Roger Riggs added a comment -
          Printing a time nearly always requires the timezone to be known and available.
          The LocalDateTime does not have a field or value for the timezone.
          The program shows different behaviors in different locales because the locale specific pattern selected may or may not include a pattern letter than prints the timezone or zone offset. Those patterns including the letters: V, z, O, X, or, x require a timezone.

          ZonedDateTime and OffsetDateTime should be provided when formatting using localized FormatStyles.
          Show
          rriggs Roger Riggs added a comment - Printing a time nearly always requires the timezone to be known and available. The LocalDateTime does not have a field or value for the timezone. The program shows different behaviors in different locales because the locale specific pattern selected may or may not include a pattern letter than prints the timezone or zone offset. Those patterns including the letters: V, z, O, X, or, x require a timezone. ZonedDateTime and OffsetDateTime should be provided when formatting using localized FormatStyles.
          Show
          rriggs Roger Riggs added a comment - Review thread: http://mail.openjdk.java.net/pipermail/core-libs-dev/2016-March/039451.html
          Hide
          rriggs Roger Riggs added a comment -
          The javadoc for the localized formats is updated to described they may require a timezone and recommending
          that when using those formats a temporal with a ZoneId is needed. For example, ZonedDateTime or OffsetDateTime.
          The exception messages were enhanced to be more useful to identify the issue.
          Show
          rriggs Roger Riggs added a comment - The javadoc for the localized formats is updated to described they may require a timezone and recommending that when using those formats a temporal with a ZoneId is needed. For example, ZonedDateTime or OffsetDateTime. The exception messages were enhanced to be more useful to identify the issue.
          Hide
          hgupdate HG Updates added a comment -
          URL: http://hg.openjdk.java.net/jdk9/dev/jdk/rev/28df1af8e872
          User: rriggs
          Date: 2016-03-16 17:24:39 +0000
          Show
          hgupdate HG Updates added a comment - URL: http://hg.openjdk.java.net/jdk9/dev/jdk/rev/28df1af8e872 User: rriggs Date: 2016-03-16 17:24:39 +0000
          Hide
          hgupdate HG Updates added a comment -
          URL: http://hg.openjdk.java.net/jdk9/jdk9/jdk/rev/28df1af8e872
          User: lana
          Date: 2016-03-30 18:38:15 +0000
          Show
          hgupdate HG Updates added a comment - URL: http://hg.openjdk.java.net/jdk9/jdk9/jdk/rev/28df1af8e872 User: lana Date: 2016-03-30 18:38:15 +0000

            People

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

              Dates

              • Created:
                Updated:
                Resolved: