先来看用"YYYY"和"yyyy"进行时间格式转换的结果
(当前时间是 12月31日)
可以看到用YYYY比yyyy转换的时间多了1年
所以来研究一下Y和y
y:year,表示“年”,基于天的年
Y:week year ,表示“周年”,基于周的年
According to the Javadoc:
A week year is in sync with a WEEK_OF_YEAR cycle. All weeks between the first and last weeks (inclusive) have the same week year value. Therefore, the first and last days of a week year may have different calendar year values.
举个栗子,2021年12月27日,周一,这周跨年了,所以这周的年份是2022年
总结:大家以后进行日期转换还是乖乖用yyyy吧~
ps:
SimpleDateFormat中字母和格式的对应表
| 字母 | 日期或时间元素 | 表示 | 示例 |
|---|---|---|---|
| G | Era 标志符 | Text | AD |
| Y | 年(周) | Year | 1996; 96 |
| y | 年 | Year | 1996; 96 |
| M | 年中的月份 | Month | July; Jul; 07 |
| w | 年中的周数 | Number | 27 |
| W | 月份中的周数 | Number | 2 |
| D | 年中的天数 | Number | 189 |
| d | 月份中的天数 | Number | 10 |
| F | 月份中的星期 | Number | 2 |
| E | 星期中的天数 | Text | Tuesday; Tue |
| a | Am/pm 标记 | Text | PM |
| H | 一天中的小时数(0-23) | Number | 0 |
| k | 一天中的小时数(1-24) | Number | 24 |
| K | am/pm 中的小时数(0-11) | Number | 0 |
| h | am/pm 中的小时数(1-12) | Number | 12 |
| m | 小时中的分钟数 | Number | 30 |
| s | 分钟中的秒数 | Number | 55 |
| S | 毫秒数 | Number | 978 |
| z | 时区 | General time zone | Pacific Standard Time; PST; GMT-08:00 |
| Z | 时区 | RFC 822 time zone | -0800 |



