Java Dates and time
java.time replaced two broken APIs. Most remaining bugs are a confusion between an instant, a local time, and a zoned time.
3 concepts · 8 interview questions
What this topic covers
Every concept in dates and time, and the questions each one gets asked as. Where a question links, it has a full write-up.
The java.time types
LocalDate has no zone, Instant is a point on the timeline, and ZonedDateTime is the two combined. Picking the wrong one is the bug.
- When do you use LocalDateTime versus Instant versus ZonedDateTime?
- Why are java.time types immutable?
- What is the difference between Duration and Period?
Time zones and offsets
An offset is a number; a zone is a set of rules that changes. Only zones survive daylight saving.
- What is the difference between a ZoneId and a ZoneOffset?
- How should you store a timestamp in a database?
- What happens to a local time during a DST transition?
Legacy Date and Calendar
Date is a mutable instant with a misleading name, and SimpleDateFormat is not thread-safe — a classic shared-static bug.
- Why is SimpleDateFormat not thread-safe?
- What was wrong with Date and Calendar?