为什么 Java 的 java.time.format.DateTimeFormatter#format(LocalDateTime) 增加了一年? [英] Why does Java's java.time.format.DateTimeFormatter#format(LocalDateTime) add a year?
问题描述
这是代码:
LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(1451438792953L), ZoneId.of("UTC"));
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("YYYY-MM-dd'T'HH:mm:ss.SSS'Z'");
String output = dateTimeFormatter.format(localDateTime);
这是localDateTime
的值:
2015-12-30T01:26:32.953
这是output
的值:
2016-12-30T01:26:32.953Z
为什么要增加一年?
在java.time.temporal.WeekFields
中,有几个方法带有newYearWeek
,有时会将年份增加1
.为什么?
In java.time.temporal.WeekFields
there are a couple of methods with a newYearWeek
which increment the year by 1
on occasion. Why?
这是一个奇怪的错误.
推荐答案
来自 维基百科:
[YYYY] 表示 ISO 周编号年,略有不同于传统的公历年(见下文).
[YYYY] indicates the ISO week-numbering year which is slightly different from the traditional Gregorian calendar year (see below).
YYYY
是年份的 ISO-8601 样式表示.yyyy
是公历年代的表示法.
YYYY
is an ISO-8601 style representation of the year.yyyy
is the Gregorian year-of-era representation.
由于两者的计算可能相差+1或-1,因此格式不同.更多有用的信息请访问 YEAR_OF_ERA
, YEAR
和 weekBasedYear
.
Since the the calculation of the two can be different by +1 or -1, hence the formatting. More useful info at YEAR_OF_ERA
, YEAR
, and weekBasedYear
.
这篇关于为什么 Java 的 java.time.format.DateTimeFormatter#format(LocalDateTime) 增加了一年?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!