Java SE 8 Programmer II study materials
Java SE 8 Programmer II study resources

Java SE 8 Programmer II: Java Date/Time API

Java SE 8 Programmer II · Work through your exam, one topic at a time.

All guide sections

Java Date/Time API

Java Date/Time API tests the immutable java.time types, arithmetic, formatting, and time-zone behavior.

Core rules

  • LocalDate, LocalTime, and LocalDateTime have no time zone; ZonedDateTime and OffsetDateTime do; Instant is a UTC point on the timeline.
  • All core types are immutable; methods such as plusDays, withMonth, and truncatedTo return new objects.
  • Period represents a date-based amount (years, months, days); Duration represents a time-based amount (seconds, nanos). Use ChronoUnit for unit-based arithmetic.
  • DateTimeFormatter is immutable and thread-safe. Patterns are case-sensitive: MM is month, mm is minute, yyyy is year-of-era, HH is 24-hour hour.
  • Parsing uses LocalDate.parse(text, formatter); formatting uses date.format(formatter). Invalid text throws DateTimeParseException.
  • ZonedDateTime handles DST gaps and overlaps; withZoneSameInstant preserves the instant while changing the zone.

Worked example

LocalDate d = LocalDate.of(2020, 2, 29); LocalDate e = d.plusYears(1); Period p = Period.between(d, e); e is 2021-02-28 because 2021 is not a leap year. Period.between gives P11M30D, not P1Y: it counts 11 whole months to 2021-01-29, then 30 days to the clamped end date.

Traps

  • java.time months are 1-12, unlike the 0-based Calendar.MONTH.
  • Period is date-based; Duration is time-based and uses seconds/nanos.
  • DateTimeFormatter patterns are case-sensitive; mixing MM and mm changes meaning.
  • Instant has no zone or offset; use atZone to attach one.
  • LocalDateTime ignores DST transitions; use ZonedDateTime for zone rules.