Java Zero to All All questionsPractise free
Interview questions › Java Foundations — Zero to Basics › Java 8 — Optional, Date/Time API, Method Refs

Compare Duration and Period, and why does adding a Duration of '24 hours' differ from adding '1 day'?

Level III · MidJava 8 — Optional, Date/Time API, Method Refs
Short answer

Duration is time-based (seconds and nanos) and Period is date-based (years, months, days).

Explanation

Across a daylight-saving transition they diverge: adding a Period of 1 day to a ZonedDateTime keeps the same wall-clock time the next day (which may be 23 or 25 real hours), while adding a Duration of 24 hours advances exactly 24 hours of elapsed time, landing on a different wall-clock time. Choosing the wrong one produces scheduling bugs that surface only twice a year, which is exactly why interviewers probe it.

Common mistakes
  • Saying Duration and Period are interchangeable.
  • Forgetting daylight-saving changes make a day not always 24 hours.

Can you say this out loud in two minutes? Drill it in a timed mock interview, with flashcards and runnable code for Java 8 — Optional, Date/Time API, Method Refs.

Practise free

More on Java 8 — Optional, Date/Time API, Method Refs