首页 / JAVA / 使用DateTimeFormatter.ofLocalizedTime(FormatStyle.FULL)和LocalTime实例时的java – DateTimeException
使用DateTimeFormatter.ofLocalizedTime(FormatStyle.FULL)和LocalTime实例时的java – DateTimeException
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了使用DateTimeFormatter.ofLocalizedTime(FormatStyle.FULL)和LocalTime实例时的java – DateTimeException,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2409字,纯文字阅读大概需要4分钟。
内容图文
在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));
内容总结
以上是互联网集市为您收集整理的使用DateTimeFormatter.ofLocalizedTime(FormatStyle.FULL)和LocalTime实例时的java – DateTimeException全部内容,希望文章能够帮你解决使用DateTimeFormatter.ofLocalizedTime(FormatStyle.FULL)和LocalTime实例时的java – DateTimeException所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。