Location>code7788 >text

"YYYY" trap in Java date formatting: Why does New Year's Eve Week suddenly make your year +1? .md

Popularity:338 ℃/2025-04-27 20:35:38

Conclusions come first

Use in JavaYYYY-MM-ddWhen formatting the date, if the day is on the New Year's Eve, the year may be incorrectly calculated as the next year (such as December 26, 2021 as 2022). and useyyyy-MM-ddThe correct year will always be returned. The root of the problem isYYYYandyyyyDifferent definitions of years:YYYYis a "week-based year", andyyyyIt is a "natural year".

Articles are updated continuously, you can search on WeChat for "Half a head"Read as soon as possible


Problem recurs

Try formatting with the following code2021-12-26

import ;
 import ;

 public class DateFormatDemo {
     public static void main(String[] args) {
         SimpleDateFormat sdf1 = new SimpleDateFormat("YYYY-MM-dd");
         SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
        
         Date date = new Date(121, 11, 26); // December 26, 2021
        
         ((date)); // Output: 2022-12-26
         ((date)); // Output: 2021-12-26
     }
 }

The results are obvious,YYYYThe year jumped to 2022.


Cause analysis

1. YYYYIt is a "week-based year"
  • definitionYYYYFollow ISO 8601 standards, indicating "year to which the date week belongs."
  • Weekly calculation rules
    • Default one week fromStarting Sunday,arriveEnded on Saturday(Available throughLocaleAdjustments, such as some areas starting Monday).
    • If a week eves the New Year (that is, the week includes the first day of the New Year), the week will be attributed to the year when the New Year is located.

byDecember 26, 2021As an example:

  • This weekDecember 26, 2021 (Sunday) to January 1, 2022 (Saturday)
  • Since most days of this week (4 days) belong to 2022,YYYYIt will be attributed to 2022.
2. yyyyIt is a "natural year"
  • definitionyyyyDirectly represent the natural year where the date is located, and has nothing to do with the week.
  • Always return to the actual year regardless of whether the date spans a week or a month.

How to avoid trampling on pitfalls?

  1. Priority useyyyy
    Unless it is explicitly necessary to calculate based on the year of the week, the date should be used when formattingyyyy

  2. Understand business scenarios

    • Financial systems, weekly statistics, etc. may be requiredYYYY(If you generate reports by weekly).
    • Normal date scenarios (such as order time, logging) should be usedyyyy
  3. NoticeLocaleThe impact of

    • The weekly start days are different in different regions (for example, the United States starts on Sunday by default, and Europe starts on Monday).
    • Available()or()Specify the area.
  4. Alternatives to Java 8+
    useDateTimeFormatterhour,yyyycorrespondYanduBehavior is clearer:

    LocalDate date = (2021, 12, 26);
    DateTimeFormatter formatter1 = ("YYYY-MM-dd");
    DateTimeFormatter formatter2 = ("yyyy-MM-dd");
    
    ((formatter1)); // 2022-12-26
    ((formatter2)); // 2021-12-26
    

Summarize

  • YYYYandyyyyThe difference: Year based on week vs natural year.
  • root cause: The rules of the New Year’s Eve Week lead toYYYYyear offset.
  • Best Practices: If there are no special needs, always use ityyyy-MM-ddFormat date.

A small format character difference may lead to serious logical errors during the New Year's Eve. Remember:Nothing trivial to date formatting!