gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:54:40 24 4
gpt4 key购买 nike

在 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 - 此格式样式适用于 LocalDateLocalDateTime 实例。但是使用 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 以反射(reflect)这一点(对异常消息进行了一些额外的改进):

@@ -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 实例,它会无一异常(exception)地工作:

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

关于java - 将 DateTimeFormatter.ofLocalizedTime(FormatStyle.FULL) 与 LocalTime 实例一起使用时出现 DateTimeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48731863/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com