All topics
library
beginner

LocalDate, LocalTime & LocalDateTime

Use the modern date/time API (Java 8+) that replaced the broken Date/Calendar classes.

java.time replaced Date and Calendar in Java 8. Everything in it is immutable.

Old Date = a pencil-written note on a shared whiteboard (anyone can erase and rewrite). LocalDate = a printed, laminated card (immutable, clear, each person gets their own copy).

Key Concepts

1
    LocalDate  d  = LocalDate.of(2026, 3, 15);        // a date, no time
    LocalTime  t  = LocalTime.of(14, 30);              // a time, no date
    LocalDateTime dt = LocalDateTime.of(d, t);         // both, no zone
    ZonedDateTime z  = dt.atZone(ZoneId.of("Asia/Kolkata"));
    Instant now = Instant.now();                       // a point on the timeline
2
Picking the right one is most of the skill.
    LocalDate       a birthday, an invoice date
    LocalTime       a shop opening time
    LocalDateTime   a wall-clock reading with no zone -- use carefully
    ZonedDateTime   an appointment in a real place
    Instant         a timestamp, for logs and storage
3
LocalDateTime is the one people misuse. It has no zone, so it cannot say when something actually happened. Store an Instant and convert for display.
LocalDateTimeInstant
4
Immutable means methods return new objects:
    LocalDate d = LocalDate.of(2026, 1, 31);
    d.plusMonths(1);        // returns 2026-02-28; d is unchanged
    d = d.plusMonths(1);    // what you meant
5
Note the month arithmetic clamps to the last valid day rather than overflowing.
6
Why the old API was broken.
    Date was mutable          and so never thread-safe
    months were 0-indexed     January was 0
    years were offset by 1900
    Date was really a timestamp, named as if it were a date
    SimpleDateFormat was not thread-safe, and was routinely
      used as a static field
7
Period and Duration measure amounts, and the split matters:
PeriodDuration
8
    Period.between(d1, d2);          // years, months, days
    Duration.between(t1, t2);        // hours, minutes, seconds, nanos
    ChronoUnit.DAYS.between(d1, d2); // a single unit, as a long
9
Period is calendar-aware, so a month can be 28 to 31 days. Duration is exact machine time. Adding a day of Duration across a daylight-saving change gives a different wall-clock time than adding a Period of one day — and the Period is usually what a human meant.
PeriodDuration
10
Formatting is thread-safe now:
    DateTimeFormatter.ofPattern("dd/MM/yyyy").format(d);
11
What the interviewer is probing.1. "Why was java.util.Date replaced?" Probing: whether you can list concrete faults. Stalls: "It was old." Moves up: mutable and so not thread-safe, months 0-indexed, years offset from 1900, and named Date while actually being a timestamp.
12
2. "Which type should store when an order was placed?" Probing: the commonest modelling error. Stalls: "LocalDateTime." Moves up: Instant — LocalDateTime has no zone, so it cannot identify a moment; convert to a zone only for display.
13
3. "What is the difference between Period and Duration?" Probing: calendar versus machine time. Stalls: "Both measure time." Moves up: Period is calendar-aware in years, months and days; Duration is exact seconds. Adding a day across a daylight-saving change gives different answers, and Period is usually what a human meant.
14
4. "What does LocalDate.of(2026,1,31).plusMonths(1) give?" Probing: clamping. Stalls: "An error." Moves up: 28 February — the result clamps to the last valid day rather than overflowing.