Christmas lands in January
A easy Java puzzle in the dates, time zones and dst family. One line below contains a real bug. Try to spot it first, then read why it fails and how to fix it.
Highlighted: line 6. Play this one as a puzzle without the answer.
What is wrong
In the legacy Calendar API months are zero-based, so 12 is not December: it overflows into January of the next year. The call produces 25 January 2026 without any error.
The fix
How to avoid it next time
Use java.time (LocalDate.of(2025, 12, 25)), where months run 1 to 12, or the Calendar constants.
Want the background? Read Legacy Date, Calendar and SimpleDateFormat in the Java concepts library.
About dates, time zones and dst
Local dates and times hide the zone they belong to. Daylight saving changes the length of a day, a month has varying lengths and the week-year differs from the calendar year around New Year.
In code review: Ask which zone each date is in and whether the code uses an instant or a local time. Prefer java.time with an explicit ZoneId over Date and SimpleDateFormat.
More puzzles in this family
Frequently asked questions
What is the bug in "Christmas lands in January"?
In the legacy Calendar API months are zero-based, so 12 is not December: it overflows into January of the next year. The call produces 25 January 2026 without any error. The faulty line is line 6: c.set(2025, 12, 25);
How do I fix it?
Replace it with: c.set(2025, Calendar.DECEMBER, 25);. Use java.time (LocalDate.of(2025, 12, 25)), where months run 1 to 12, or the Calendar constants.
How do I catch dates, time zones and dst in code review?
Ask which zone each date is in and whether the code uses an instant or a local time. Prefer java.time with an explicit ZoneId over Date and SimpleDateFormat.
Is there a new puzzle every day?
Yes. The daily Spot the Bug puzzle picks one of these snippets for each UTC day, with the difficulty set by the weekday. Your streak is stored only in your browser.