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

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

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 9
    • 8u45
    • core-libs
    • b112
    • x86
    • os_x

      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

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

              Created:
              Updated:
              Resolved: