踩坑: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.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM