ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

使用DateTimeFormatter.ofLocalizedTime(FormatStyle.FULL)和LocalTime实例时的java – DateTimeException

2019-06-11 04:47:07  阅读:463  来源: 互联网

标签:java java-8 datetime-format java-date


Java 8 Date Time API中,我将使用DateTimeFormatter API打印时间,如下所示:

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

FormatStyle.FULL – 此格式样式适用于LocalDate和LocalDateTime实例.但是使用LocalTime实例抛出异常:

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

根据文件:

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));

标签:java,java-8,datetime-format,java-date
来源: https://codeday.me/bug/20190611/1216325.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有