为什么Java的 java.time.format.DateTimeFormatter#format(LocalDateTime) 加一年? [英] Why does Java's java.time.format.DateTimeFormatter#format(LocalDateTime) add a year?

查看:27
本文介绍了为什么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).

  1. YYYY 是年份的 ISO-8601 样式表示.
  2. yyyy 是公历年份表示.
  1. YYYY is an ISO-8601 style representation of the year.
  2. yyyy is the Gregorian year-of-era representation.

由于两者的计算可以相差+1或-1,因此格式.更多有用信息请参见 YEAR_OF_ERA, YEARweekBasedYear.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆