首先在1月份使用DateTimeFormatter会导致年份值无效 [英] Using DateTimeFormatter on january first cause an invalid year value
问题描述
当我在设置为2017年1月1日的LocalDate上使用DateTimeFormatter(代码低于)时,我的结果是错误的年份值。
When i'm using a DateTimeFormatter (code bellow) on a LocalDate set to January 1st 2017 i am ending with a wrong year value.
LocalDate.of(2017,1,1).toString
这将正确显示2017-01-01 while
This will properly display 2017-01-01 while
LocalDate.of(2017,1,1).format(DateTimeFormatter.ofPattern("dd MMM YYYY"))
给我:
01 janv。 2016.你知道为什么会这样吗?
Is giving me: 01 janv. 2016. Do you have any idea why this is happening?
推荐答案
你正在使用错误的年份。
Your are using the "wrong" year.
Symbol Meaning Presentation Examples
------ ------- ------------ -------
y year-of-era year 2004; 04
Y week-based-year year 1996; 96
资料来源: DateTimeFormatter
这基本上是使用的第一天本周看看它是在哪一年而不是实际的一天。
This is basicly using the first day of the current week to see in what year it was instead of the actual day.
其中一个问题是这是本地依赖...
One of the issues is that this is Local dependant...
您应该使用 DateTimeFormatter.ofPattern(dd MMM yyyy)
。
这篇关于首先在1月份使用DateTimeFormatter会导致年份值无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!