Unravel โบ Java concepts โบ Time and dates
java.time dates and times
The java.time classes separate a point on the timeline (Instant), a calendar date or time without a zone (LocalDate, LocalDateTime) and a zoned moment (ZonedDateTime).
At a glance
Instant is a stopwatch reading shared by the whole world; LocalDateTime is what a wall clock shows with no country attached; ZonedDateTime is the wall clock in a specific city.
- 1Store and exchange moments as Instant (UTC) or ISO-8601 stringsConvert to ZonedDateTime with a ZoneId only when showing to a person.
- 2LocalDate is a date with no time or zone (a birthday)LocalDateTime has no zone, so it does not identify a moment; do not store it for events.
- 3Duration measures time-based amounts (hours, seconds) and Period date-based amounts (months, days)They are immutable; plus returns a new object.
- 4DateTimeFormatter formats and parsesIt is thread safe, unlike the old SimpleDateFormat.
- Daylight saving makes "add one day" differ from "add 24 hours" in a zone
- An Instant is seconds plus nanoseconds since the epoch
Instant now = clock.instant();ZonedDateTime inTokyo = now.atZone(ZoneId.of("Asia/Tokyo"));LocalDate tomorrow = LocalDate.now(clock).plusDays(1);Duration timeout = Duration.ofSeconds(30);String text = DateTimeFormatter.ISO_LOCAL_DATE.format(tomorrow);- line 1 uses Instant, the core of java.time dates and times
- line 2 uses ZonedDateTime, the core of java.time dates and times
- line 3 uses LocalDate, the core of java.time dates and times
- line 4 uses Duration, the core of java.time dates and times
- line 5 uses DateTimeFormatter, the core of java.time dates and times
Read the full explanation (about 1 min)
The java.time classes separate a point on the timeline (Instant), a calendar date or time without a zone (LocalDate, LocalDateTime) and a zoned moment (ZonedDateTime). Choosing the right one avoids time zone bugs.
A mental model
Instant is a stopwatch reading shared by the whole world; LocalDateTime is what a wall clock shows with no country attached; ZonedDateTime is the wall clock in a specific city.
How it works
Store and exchange moments as Instant (UTC) or ISO-8601 strings. Convert to ZonedDateTime with a ZoneId only when showing to a person.
LocalDate is a date with no time or zone (a birthday). LocalDateTime has no zone, so it does not identify a moment; do not store it for events.
Duration measures time-based amounts (hours, seconds) and Period date-based amounts (months, days). They are immutable; plus returns a new object.
DateTimeFormatter formats and parses. It is thread safe, unlike the old SimpleDateFormat.
Going deeper
Daylight saving makes "add one day" differ from "add 24 hours" in a zone. A local time can be skipped (spring forward) or occur twice (fall back). Inject a Clock instead of calling now() directly so tests control the time.
Under the hood
An Instant is seconds plus nanoseconds since the epoch. Zone rules come from the tz database bundled with the JDK, so an old JDK can have stale rules.
Common mistakes
- Storing LocalDateTime for real events.
- Adding 24 hours when meaning "next day".
- Calling
now()everywhere, which makes code untestable. - The legacy
DateandSimpleDateFormat.
How Unravel recognises it
When you paste code, Unravel looks for these signs of java.time dates and times. Spotting them yourself is a good habit when you read unfamiliar code:
- Types:
LocalDateLocalDateTimeLocalTimeZonedDateTimeInstantDurationPeriodZoneIdOffsetDateTimeDateTimeFormatterClockChronoUnit
Part of a learning path
- Modeling data in modern Java ยท previous: Switch expressions and pattern matching ยท next: BigDecimal and money
Builds on this
Frequently asked questions
What is java.time dates and times?
The java.time classes separate a point on the timeline (Instant), a calendar date or time without a zone (LocalDate, LocalDateTime) and a zoned moment (ZonedDateTime). Choosing the right one avoids time zone bugs.
What are the most common mistakes with java.time dates and times?
Storing LocalDateTime for real events. Adding 24 hours when meaning "next day". Calling now() everywhere, which makes code untestable. The legacy Date and SimpleDateFormat.
How can I see java.time dates and times in real code?
Paste your own Java into Unravel and it will point out where java.time dates and times appears, explain how it works at the detail level you choose, and draw the flow of the code around it.