将 DateTimeFormatter.ofLocalizedTime(FormatStyle.FULL) 与 LocalTime 实例一起使用时出现 DateTimeException

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

在 Java 8 日期时间 API 中,我将使用 DateTimeFormatter API 打印时间,如下所示:

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

FormatStyle.FULL - 这种格式样式适用于 LocalDateLocalDateTime 实例。但是用 LocalTime 实例抛出异常:

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

根据文档:

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,

为什么会抛出异常?

看起来你被 JDK-JDK-8085887: java.time.format.FormatStyle.LONG or FULL causes unchecked exception 击中了(已在 ​​JDK 9 中修复)。

异常背后的原因在第一条评论中说明:

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.

查看差异时(例如 DateTimeFormatter),您可以看到他们只是更新了 javadoc 以反映这一点(对异常消息进行了一些额外的改进):

@@ -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) {

如果您将时区添加到您的 DateTimeFormatter 实例,它毫无例外地工作:

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