問題描述
這是代碼:
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
.
這篇關于為什么Java的 java.time.format.DateTimeFormatter#format(LocalDateTime) 加一年?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!