这对我来说不是很清楚。出于某种原因,当我尝试使用 格式化LocalDateTime实例DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG).withLocale(...)时,出现异常:
java.time.DateTimeException:无法提取值:类 java.time.LocalDateTime
例如,只有在我使用FormatStyle.LONG,时才会发生这种情况FormatStyle.MEDIUM。
这是我的测试:
@Test
public void dateTest() {
LocalDateTime now = LocalDateTime.now();
// this is ok. prints a value
System.out.println("LocalDateTime now (formatted with locale): "
+ now.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM)
.withLocale(new Locale("it"))));
// this fails with java.time.DateTimeException: Unable to extract value: class java.time.LocalDateTime
// only if FormatStyle.LONG (as it is now)
System.out.println("LocalDateTime now (formatted with locale): "
+ now.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG)
.withLocale(new Locale("it"))));
}
对此有什么好的解释吗?
湖上湖
杨__羊羊
相关分类