Why Using ‘YYYY’ Instead of ‘yyyy’ Can Break Your Java Dates
A subtle mistake of using the week‑based year pattern ‘YYYY’ instead of the calendar year ‘yyyy’ in Java DateTimeFormatter can cause unexpected date bugs, especially around year boundaries, and this article explains the symptoms, root cause, and how to avoid it.
Yesterday on v2ex I encountered a bug caused by using the pattern YYYY -MM-dd in Java date formatting. Similar issues often appear when details are overlooked, leading to unexpected behavior in specific times or environments.
Whether you have faced this bug or not, after reading this article you should pay attention to this detail to avoid future mistakes.
Bug Manifestation
Bug Cause
The pattern YYYY represents the week‑based‑year. For example, today is already considered year 2020 according to this rule, while yyyy would still be 2019.
YYYY means the year of the week to which the day belongs; a week starts on Sunday and ends on Saturday, and if a week spans the new year, that week is counted as part of the next year.
Related Resources
Java DateTimeFormatter Patterns Documentation
Article on Y2K‑like bugs
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
