java日期Date转换增加1年:SimpleDateFormat中YYYY和yyyy的区别

java日期Date转换增加1年:SimpleDateFormat中YYYY和yyyy的区别,第1张

java日期Date转换增加1年:SimpleDateFormat中YYYY和yyyy的区别

先来看用"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中字母和格式的对应表

字母日期或时间元素表示示例GEra 标志符TextADY年(周)Year1996; 96y年Year1996; 96M年中的月份MonthJuly; Jul; 07w年中的周数Number27W月份中的周数Number2D年中的天数Number189d月份中的天数Number10F月份中的星期Number2E星期中的天数TextTuesday; TueaAm/pm 标记TextPMH一天中的小时数(0-23)Number0k一天中的小时数(1-24)Number24Kam/pm 中的小时数(0-11)Number0ham/pm 中的小时数(1-12)Number12m小时中的分钟数Number30s分钟中的秒数Number55S毫秒数Number978z时区General time zonePacific Standard Time; PST; GMT-08:00Z时区RFC 822 time zone-0800

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5686222.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存