java.time.DateTimeException:无法从临时文件中提取ZoneId
当我运行第一段时,它非常好,并生成一个输出。但在第二种情况下,当我运行这个段2时,它会生成java.time.DateTimeException:无法从临时文件中提取ZoneId,java,datetime-format,java-time,Java,Datetime Format,Java Time,当我运行第一段时,它非常好,并生成一个输出。但在第二种情况下,当我运行这个段2时,它会生成 DateTimeException : Unable to extract ZoneId from temporal. 第1部分: 第2部分: 我认为解释起来有点复杂,因为你把两件事混为一谈,一件是CalizedDate的,一件是CalizedDateTime的,另一件是格式样式的: 在第一种情况下,您使用FormatStyle.FULL调用calizeddate的,因此忽略了时间部分 在第二种情况下,
DateTimeException : Unable to extract ZoneId from temporal.
第1部分:
第2部分:
我认为解释起来有点复杂,因为你把两件事混为一谈,一件是CalizedDate的
,一件是CalizedDateTime的,另一件是格式样式的:
在第一种情况下,您使用FormatStyle.FULL调用calizeddate的,因此忽略了时间部分
在第二种情况下,您也使用FormatStyle.FULL
调用了CalizedDateTime的,它将包括日期的所有部分,而LocalDate
或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;
要继续,我们可以在此表中创建一个
定位时间
钙矾土
校准时间
LocalTime
中号,短号
LocalDate
全、长、中、短
LocalDateTime
中号,短号
全、长、中、短
中号,短号
ZoneDateTime
全、长、中、短
全、长、中、短
全、长、中、短
OffsetDateTime
中号,短号
全、长、中、短
中号,短号
您混淆了“本地化”和“本地”:
- :返回特定于区域设置的日期时间格式化程序
- :没有时区的日期时间
正如你所看到的,它们是两个完全不同的术语
现在,尝试提供一个值,这样您就可以了解它为什么需要一个值
输出(地区:en\u-US
,时区:America/New\u-York
)
2019年12月30日星期一东部标准时间上午8:09:16
如您所见,它需要时区来知道时间是“东部标准时间”
如果将时间样式从减少到,则不再需要时区
LocalDateTime ldt = LocalDateTime.now();
DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL, FormatStyle.MEDIUM);
System.out.println(dtf.format(ldt));
输出
2019年12月30日星期一上午8:09:16
请详细解释为什么要使用extra thinkZonedDateTime@NgSharma好的,现在检查我的答案,我想我会让它更容易,更清楚,参考
/**
* 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;
ZonedDateTime zdt = ZonedDateTime.now();
DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL);
System.out.println(dtf.format(zdt));
LocalDateTime ldt = LocalDateTime.now();
DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL, FormatStyle.MEDIUM);
System.out.println(dtf.format(ldt));