Programmer DD
Programmer DD
Dec 26, 2021 · Backend Development

Why 'YYYY-MM-dd' Gives the Wrong Year in Java – The Week‑Based Year Pitfall

The article explains why using the pattern 'YYYY‑MM‑dd' with Java's SimpleDateFormat can produce an incorrect year for dates near the end of the year, demonstrates the issue with a unit test comparing it to the correct 'yyyy‑MM‑dd' pattern, and reveals that the problem stems from the week‑based year semantics of 'Y'.

Date FormattingJavaSimpleDateFormat
0 likes · 5 min read
Why 'YYYY-MM-dd' Gives the Wrong Year in Java – The Week‑Based Year Pitfall
Programmer DD
Programmer DD
Dec 30, 2020 · Backend Development

Why Using YYYY‑MM‑dd Can Produce Wrong Years in Java Date Formatting

This article demonstrates how the week‑based year pattern "YYYY" in Java's SimpleDateFormat can incorrectly roll over the year for dates near year‑end, provides a reproducible unit test with code and output, and explains the underlying reason behind the discrepancy.

Date FormattingSimpleDateFormatweek-based-year
0 likes · 4 min read
Why Using YYYY‑MM‑dd Can Produce Wrong Years in Java Date Formatting