java - java.time.DateTimeException:无法从时间中提取 ZoneId
问题描述
当我运行第一段时,它非常好并生成输出。但是在第二种情况下,当我运行此段 2 时,它会生成
DateTimeException : Unable to extract ZoneId from temporal.
第 1 部分:
LocalDate ld = LocalDate.now();
System.out.println(DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).format(ld));
第 2 部分:
LocalDateTime ldt = LocalDateTime.now();
DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL);
System.out.println(dtf.format(ldt));
解决方案
我认为解释起来有点复杂,因为您在两件事之间混合ofLocalizedDate
,ofLocalizedDateTime
并且FormatStyle
:
在第一种情况下,您ofLocalizedDate
使用 the 调用,FormatStyle.FULL
因此您忽略了时间部分。
在第二种情况下,您还调用ofLocalizedDateTime
whichFormatStyle.FULL
将包括日期的所有部分,而LocalDate
or则不是这种情况LocalDateTime
。
可以肯定的是,让我们尝试使用MEDIUM
,或SHORT
代替FULL
:
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM).format(ldt)
=> 30 déc. 2019 à 14:57:40 - without any exception
有关更多详细信息,请查看此处的评论:
/**
* Full text style, with the most detail.
* For example, the format might be 'Tuesday, April 12, 1952 AD' or '3:30:42pm PST'.
*/
FULL,
/**
* Long text style, with lots of detail.
* For example, the format might be 'January 12, 1952'.
*/
LONG,
/**
* Medium text style, with some detail.
* For example, the format might be 'Jan 12, 1952'.
*/
MEDIUM,
/**
* Short text style, typically numeric.
* For example, the format might be '12.13.52' or '3:30pm'.
*/
SHORT;
要恢复,我们可以创建一个此表:
本地化时间 | ofLocalizedDate | ofLocalizedDateTime | |
---|---|---|---|
当地时间 | 中,短 | ||
本地日期 | 全、长、中、短 | ||
本地日期时间 | 中,短 | 全、长、中、短 | 中,短 |
分区日期时间 | 全、长、中、短 | 全、长、中、短 | 全、长、中、短 |
偏移日期时间 | 中,短 | 全、长、中、短 | 中,短 |
=> FULL, LONG, MEDIUM, SHORT are FormatStyle
您可以阅读它,因为它LocalDateTime
可以ofLocalizedDate
与所有格式样式一起使用,并且不能接受任何FormatStyle
与ofLocalizedDateTime
推荐阅读
- usb - 如何在没有用户手势的情况下重新连接到 USB 设备?
- reactjs - 想要同时在下一个选项卡上打开一个 URL 并导航到一个路由:React+ React Router V4+ semantic-ui-react
- java - Java是否以大端位顺序读取单个字节?
- arrays - Delphi - 隐式类型转换运算符不适用于方法参数
- html - 如何在 swift 4 中的 webview 中显示大量数据
- php - 如果我有使用 word 的 url,如何从 php 打开 doc 或 pdf 文件?
- asp.net-core - 如何在 ASP.NET Core 2.0 中使用 SpeechSynthesizer
- unity3d - 使用 api 23 以上的统一 android 在 pdf 查看器中打开 pdf 文件
- php - 我们如何计算记录数并在rest api中显示?
- java - 为什么 KeyEvent getRepeatCount 总是返回 0?