踩坑:java时间格式化yyyy与YYYY的区别

查询数据时发现一个bug,前端传了2021-12-28这个日期,转成String类型居然变成了2022-12-28.

经过各种排查,锁定问题可能出现在一个时间转换代码上
DateFormat formatter = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");

经过百度发现YYYY代表的含义,和yyyy是不一样的

简单来说,一年有52周,超过52周的,年份+1,2021年有52个周,28号属于第53周,所以会出现年份+1

Java's DateTimeFormatter pattern "YYYY" gives you the week-based-year, (by default, ISO-8601 standard) the year of the Thursday of that week.

The unit that represents week-based-years for the purpose of addition and subtraction.
This allows a number of week-based-years to be added to, or subtracted from, a date. The unit is equal to either 52 or 53 weeks. The estimated duration of a week-based-year is the same as that of a standard ISO year at . 365.2425 Days

The rules for addition add the number of week-based-years to the existing value for the week-based-year field. If the resulting week-based-year only has 52 weeks, then the date will be in week 1 of the following week-based-year.

posted @   InkYi  阅读(1159)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗
点击右上角即可分享
微信分享提示