JDK-8085887 : java.time.format.FormatStyle.LONG or FULL causes unchecked exception
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.time
  • Affected Version: 8u45
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: os_x
  • CPU: x86
  • Submitted: 2015-06-04
  • Updated: 2024-08-29
  • Resolved: 2016-03-16
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 9
9 b112Fixed
Related Reports
Relates :  
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


Comments
URL: http://hg.openjdk.java.net/jdk9/jdk9/jdk/rev/28df1af8e872 User: lana Date: 2016-03-30 18:38:15 +0000
30-03-2016

URL: http://hg.openjdk.java.net/jdk9/dev/jdk/rev/28df1af8e872 User: rriggs Date: 2016-03-16 17:24:39 +0000
16-03-2016

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.
15-03-2016

Review thread: http://mail.openjdk.java.net/pipermail/core-libs-dev/2016-March/039451.html
15-03-2016

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.
11-03-2016