Ng *_*rma 2 java datetime-format java-time
当我运行第一段时,它非常好并生成一个输出。但是在第二种情况下,当我运行此段 2 时,它会生成
DateTimeException : Unable to extract ZoneId from temporal.
Run Code Online (Sandbox Code Playgroud)
第 1 部分:
LocalDate ld = LocalDate.now();
System.out.println(DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).format(ld));
Run Code Online (Sandbox Code Playgroud)
第 2 部分:
LocalDateTime ldt = LocalDateTime.now();
DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL);
System.out.println(dtf.format(ldt));
Run Code Online (Sandbox Code Playgroud)
我认为解释起来有点复杂,因为您混合了两件事ofLocalizedDate,ofLocalizedDateTime并且FormatStyle:
在第一种情况下,您正在调用ofLocalizedDate,FormatStyle.FULL因此您忽略了时间部分。
在第二种情况下,您还调用ofLocalizedDateTimewith FormatStyle.FULLwhich 将包括日期的所有部分,而LocalDateor不是这种情况LocalDateTime。
为了确保让我们尝试使用MEDIUM, 或SHORT代替FULL:
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM).format(ldt)
=> 30 déc. 2019 à 14:57:40 - without any exception
Run Code Online (Sandbox Code Playgroud)
有关更多详细信息,请查看此处的评论:
/**
* 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;
Run Code Online (Sandbox Code Playgroud)
要恢复,我们可以创建一个这个表:
| 本地化时间 | 本地化日期 | 本地化日期时间 | |
|---|---|---|---|
| 当地时间 | 中、短 | ||
| 本地日期 | 全、长、中、短 | ||
| 本地日期时间 | 中、短 | 全、长、中、短 | 中、短 |
| 分区日期时间 | 全、长、中、短 | 全、长、中、短 | 全、长、中、短 |
| 偏移日期时间 | 中、短 | 全、长、中、短 | 中、短 |
=> FULL, LONG, MEDIUM, SHORT are FormatStyle
Run Code Online (Sandbox Code Playgroud)
您可以阅读它,因为它LocalDateTime可以ofLocalizedDate与所有格式样式一起使用,并且不能接受任何FormatStyle与ofLocalizedDateTime
| 归档时间: |
|
| 查看次数: |
2197 次 |
| 最近记录: |