library
beginnerLocalDate, LocalTime & LocalDateTime
Use the modern date/time API (Java 8+) that replaced the broken Date/Calendar classes.
The java.time package (Java 8, JSR 310) provides immutable, thread-safe date/time classes that replaced the error-prone java.util.Date and Calendar.
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
Core classes:
- LocalDate: date without time (2024-03-15). No timezone.
- LocalTime: time without date (14:30:00). No timezone.
- LocalDateTime: date + time (2024-03-15T14:30:00). No timezone.
- ZonedDateTime: date + time + timezone (2024-03-15T14:30:00+01:00[Europe/Paris]).
- Instant: a point on the timeline (epoch milliseconds). Used for timestamps.
2
All are immutable: methods like plusDays(), withMonth() return new objects.
3
Creation: LocalDate.now(), LocalDate.of(2024, 3, 15), LocalDate.parse("2024-03-15").
4
Why Date/Calendar were broken:
- Date is mutable and not thread-safe
- Months are 0-indexed (January = 0)
- Year handling is offset from 1900
- No separation of date vs time vs datetime
- Calendar.MONTH vs Date.getMonth() inconsistencies
5
Period (years/months/days) and Duration (hours/minutes/seconds/nanos) represent amounts of time. ChronoUnit provides units for calculation.