在 1 月首先使用 DateTimeFormatter 会导致无效的年份值

Rom*_*ran 1 java

当我在设置为 2017 年 1 月 1 日的 LocalDate 上使用 DateTimeFormatter(代码如下)时,我以错误的年份值结束。

LocalDate.of(2017,1,1).toString
Run Code Online (Sandbox Code Playgroud)

这将正确显示 2017-01-01 而

LocalDate.of(2017,1,1).format(DateTimeFormatter.ofPattern("dd MMM YYYY"))
Run Code Online (Sandbox Code Playgroud)

给我:01 janv。2016. 你知道为什么会这样吗?

Axe*_*elH 6

您使用的是“错误”的年份。

 Symbol  Meaning            Presentation    Examples  
 ------  -------            ------------    -------  
 y       year-of-era        year            2004; 04  
 Y       week-based-year    year            1996; 96
Run Code Online (Sandbox Code Playgroud)

来源:DateTimeFormatter

这基本上是使用当前一周的第一天来查看它是哪一年而不是实际日期。

问题之一是这是本地依赖...

你应该用过DateTimeFormatter.ofPattern("dd MMM yyyy")