Quick revision
The short answer
Uppercase Y in a pattern means week-based year, not calendar year. 2024-12-30 is a Monday that belongs to week 1 of 2025 under ISO week rules, so YYYY prints 2025. The pattern should use lowercase yyyy, or uuuu, or better DateTimeFormatter.ISO_LOCAL_DATE; similar mix-ups are D for day of year instead of d, and hh for the 1 to 12 hour instead of HH.
What to remember
In this answer, we saw that uppercase Y means week-based year, in which each week belongs to a single year, so 30 December 2024, which is in week 1 of 2025, prints as 2025. Lowercase y or u means calendar year, and DateTimeFormatter.ISO_LOCAL_DATE avoids patterns altogether. The week rules depend on the locale, so the affected days vary. We also saw similar traps with D and d, hh and HH, and mm and MM, and why tests should include dates at the turn of the year.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Uppercase Y in a pattern means week-based year, not calendar year. 2024-12-30 is a Monday that belongs to week 1 of 2025 under ISO week rules, so YYYY prints 2025. The pattern should use lowercase yyyy, or uuuu, or better DateTimeFormatter.ISO_LOCAL_DATE; similar mix-ups are D for day of year instead of d, and hh for the 1 to 12 hour instead of HH.
Why this question is asked
This bug is famous because it hides for 51 weeks a year. Every test in spring, summer and autumn passes, and then in the last days of December, invoices, file names and booking confirmations jump a whole year ahead. Interviewers ask it to see whether we read pattern letters carefully and understand the week-based calendar behind them. In this answer, we will reproduce the bug, explain what a week-based year is, see why the end of December is affected, fix the pattern, and look at the other pattern letters that cause the same kind of trouble.
1Reproducing the Bug
The pattern looks right at a glance, and it gives the expected result for almost every date.
Notice that only the year is wrong; the month and day are fine. That is a clue that the problem is in what the year letter means, not in the date itself. These results are for an ISO week rules locale such as the UK; as we will see, the exact affected days depend on the locale.
2A Quick Recap: What a Week-Based Year Is
Some businesses count time in numbered weeks rather than dates: 'delivery in week 1', 'sprint 52'. For that, weeks must belong to exactly one year, even though a week can straddle 31 December and 1 January. The week-based year solves this by assigning each whole week to one year.
Under ISO-8601 rules, weeks start on Monday, and week 1 of a year is the week that contains that year's first Thursday. In 2025, 1 January is a Wednesday, so the week from Monday 30 December 2024 to Sunday 5 January 2025 contains Thursday 2 January and is week 1 of week-based year 2025. So 30 and 31 December 2024 belong to week-based year 2025, while their calendar year is still 2024. The same effect can happen at the start of January in other years: 1 January 2027 is a Friday, so it belongs to the last week of week-based year 2026.
| Date | Day | Calendar year (y) | Week-based year (Y) |
|---|---|---|---|
| 2024-12-28 | Saturday | 2024 | 2024 (week 52) |
| 2024-12-29 | Sunday | 2024 | 2024 (week 52) |
| 2024-12-30 | Monday | 2024 | 2025 (week 1) |
| 2024-12-31 | Tuesday | 2024 | 2025 (week 1) |
| 2025-01-01 | Wednesday | 2025 | 2025 (week 1) |
3Why the Formatter Printed 2025
In a DateTimeFormatter pattern, each letter names a field. Lowercase y is year of era, the ordinary calendar year. Uppercase Y is the week-based year described above. So YYYY-MM-dd asks for the week-based year, then the calendar month and the calendar day of month, which is a combination that makes no sense, but the formatter does exactly what it is told.
There is one more twist. Which weeks count as week 1 depends on the locale's week definition, because Y uses the formatter's locale. In the United States, weeks start on Sunday and week 1 is the one containing 1 January, so there 29 December 2024 is already in week-based year 2025. That is why the bug sometimes appears on different days on different servers.
4The Fix
For a calendar date we use the calendar year. The simplest fix is lowercase yyyy. In java.time, uuuu is often preferred, because u is the proleptic year, which can be negative and needs no era, and it behaves better with strict parsing. Best of all, for machine-readable dates, we use the built-in ISO formatter, which cannot be mistyped and is strict when parsing.
To keep it fixed, we add a unit test with 30 December and 1 January dates for every pattern-based formatter, and a code search or lint rule that flags YYYY in patterns. Some static analysis tools already warn about it.
5Other Pattern Letters That Look Alike
The same kind of mistake happens with other letter pairs, where the uppercase and lowercase versions mean different fields. Each produces correct-looking output on some dates and wrong output on others, so tests with a single date often miss them. The table lists the usual offenders, with what each prints for 5 February 2024 at 15:04.
| Letter | Means | Prints for 2024-02-05 15:04 | Usually wanted |
|---|---|---|---|
| Y | week-based year | 2024 (2025 in late Dec) | y or u: calendar year |
| D | day of year | 36 | d: day of month, 05 |
| hh | hour 1 to 12 | 03 | HH: hour 0 to 23, 15 |
| mm | minute | 04 | MM: month, 02 |
| S | fraction of second | 0 | s: second |
Notice that hh without an AM/PM marker is especially sneaky: 03 could be 3 in the morning or 3 in the afternoon, and the reader cannot tell. If we really want a 12-hour clock, the pattern needs the letter a as well.
6Follow-up questions
When is Y the right letter to use?
When we are formatting a week reference, together with w for the week number, as in a pattern for '2025-W01'. A week-based year only makes sense next to a week number, never next to a month and day. For ISO week dates there is also the built-in DateTimeFormatter.ISO_WEEK_DATE.
Did SimpleDateFormat have the same problem?
Yes. In SimpleDateFormat, Y is also the week year, so YYYY-MM-dd fails in the same way in legacy code. The mix-ups between D and d, and between hh and HH, apply there too.
Why might a test pass on a developer's laptop but fail in CI on the same date?
Because the week definition comes from the locale. If the laptop and the CI server have different default locales, YYYY can switch years on different days. Passing an explicit locale, or using ISO formatters, removes that dependency.
7Conclusion
In this answer, we saw that uppercase Y means week-based year, in which each week belongs to a single year, so 30 December 2024, which is in week 1 of 2025, prints as 2025. Lowercase y or u means calendar year, and DateTimeFormatter.ISO_LOCAL_DATE avoids patterns altogether. The week rules depend on the locale, so the affected days vary. We also saw similar traps with D and d, hh and HH, and mm and MM, and why tests should include dates at the turn of the year.
Open with
The pattern uses uppercase Y, which is the week-based year, not the calendar year. In a week-based calendar every week belongs to exactly one year, and under ISO rules week 1 is the week containing the year's first Thursday.
Then, if they want more
In 2025 January 1st is a Wednesday, so the week starting Monday December 30th, 2024 is week 1 of 2025. That means December 30th has calendar year 2024 but week-based year 2025, and YYYY-MM-dd prints 2025 with the calendar month and day, giving 2025-12-30.
It only happens in the last or first few days of the year, so tests usually miss it, and the exact days depend on the locale's week definition, US weeks start on Sunday for example. The fix is lowercase yyyy, or uuuu, which is the proleptic year and works better with strict parsing, or better, DateTimeFormatter.ISO_LOCAL_DATE for machine dates.
Similar traps are D, day of year, instead of d, day of month, hh, the 12-hour clock, instead of HH, and mm, minutes, instead of MM, months. I'd add tests around December 31st and January 1st for every custom pattern.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "DateTimeFormatter.ofPattern("YYYY-MM-dd") formats 2024-12-30 as 2025-12-30. What is wrong?": https://scrivio-interview-library.netlify.app/java/date-and-time/cd671317ae/