为什么Java的 java.time.format.DateTimeFormatter#format(LocalDateTime) 加一年?
这是代码:
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
这是输出
的值:
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
.
相关文章