Tomorrow at nine, except when clocks change
A hard 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 7. Play this one as a puzzle without the answer.
What is wrong
Clocks in Berlin moved forward on 30 March 2025, so that day has only 23 hours. Adding 24 elapsed hours lands at 10:00 local time, not 09:00. plusHours counts the exact time, plusDays counts calendar days.
The fix
How to avoid it next time
Use plusDays for "same time tomorrow" and plusHours for elapsed time. Test schedules around daylight-saving changes.
Want the background? Read Time zones, DST and Instant 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 "Tomorrow at nine, except when clocks change"?
Clocks in Berlin moved forward on 30 March 2025, so that day has only 23 hours. Adding 24 elapsed hours lands at 10:00 local time, not 09:00. plusHours counts the exact time, plusDays counts calendar days. The faulty line is line 7: ZonedDateTime tomorrow = today.plusHours(24);
How do I fix it?
Replace it with: ZonedDateTime tomorrow = today.plusDays(1);. Use plusDays for "same time tomorrow" and plusHours for elapsed time. Test schedules around daylight-saving changes.
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.