Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTimeException while using DateTimeFormatter.ofLocalizedTime(FormatStyle.FULL) with LocalTime instance

In Java 8 Date Time API, I am going to print time using DateTimeFormatter API like following:

DateTimeFormatter timeFormatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.FULL);
LocalTime time = LocalTime.of(12, 45, 0);
System.out.println(timeFormatter.format(time));

FormatStyle.FULL - this format style works well with LocalDate and LocalDateTime instance. But throwing an exception with LocalTime instance:

java.time.DateTimeException: Unable to extract value: class java.time.format.DateTimePrintContext$1

According to doc:

public enum FormatStyle {
    // ordered from large to small

    /**
     * Full text style, with the most detail.
     * For example, the format might be 'Tuesday, April 12, 1952 AD' or '3:30:42pm PST'.
     */
    FULL,

Why is exception throwing?

like image 255
iamcrypticcoder Avatar asked Feb 11 '18 13:02

iamcrypticcoder


People also ask

Is Java time format DateTimeFormatter thread-safe?

Yes, it is: DateTimeFormat is thread-safe and immutable, and the formatters it returns are as well.

How do I use datetime formatter?

DateTimeFormatter fmt = DateTimeFormatter. ofPattern("yyyy-MM-dd'T'HH:mm:ss"); System. out. println(ldt.

What is DateTimeFormatter Iso_date_time?

ISO_DATE_TIME. The ISO-like date-time formatter that formats or parses a date-time with the offset and zone if available, such as '2011-12-03T10:15:30', '2011-12-03T10:15:30+01:00' or '2011-12-03T10:15:30+01:00[Europe/Paris]'. static DateTimeFormatter.

What is the role of the DateTimeFormatter type?

The DateTimeFormatter class is used to both parse and format dates according to specified Date and Time Patterns. Use parse(...) method to convert from String to Date/Time classes, use format(...) method to convert from Date/Time into String.


1 Answers

Looks like you are getting hit by JDK-JDK-8085887: java.time.format.FormatStyle.LONG or FULL causes unchecked exception (fixed in JDK 9).

The reason behind the exception is stated in the first comment there:

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 comment also states that this is locale dependend due to different patterns, but that's probably not relevant for your case. I'll include it for reference, though:

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.

When looking at the diff (e.g. in DateTimeFormatter) you can see that they merely updated the javadoc to reflect that (with some additional improvements to the exception messages):

@@ -617,10 +617,13 @@
      * looking up the pattern required on demand.
      * <p>
      * The returned formatter has a chronology of ISO set to ensure dates in
      * other calendar systems are correctly converted.
      * It has no override zone and uses the {@link ResolverStyle#SMART SMART} resolver style.
+     * The {@code FULL} and {@code LONG} styles typically require a time-zone.
+     * When formatting using these styles, a {@code ZoneId} must be available,
+     * either by using {@code ZonedDateTime} or {@link DateTimeFormatter#withZone}.
      *
      * @param timeStyle  the formatter style to obtain, not null
      * @return the time formatter, not null
      */
     public static DateTimeFormatter ofLocalizedTime(FormatStyle timeStyle) {

If you add a time zone to your DateTimeFormatter instance it works without exception:

DateTimeFormatter timeFormatter = DateTimeFormatter      
                                      .ofLocalizedTime(FormatStyle.FULL)
                                      .withZone(ZoneId.systemDefault());
LocalTime time = LocalTime.of(12, 45, 0);
System.out.println(timeFormatter.format(time));
like image 189
Marvin Avatar answered Sep 18 '22 12:09

Marvin