Java Date and Time API: Working with LocalDate and LocalDateTime
Learn the modern Java Date & Time API (java.time) — LocalDate, LocalTime, LocalDateTime, date maths, comparing, formatting with DateTimeFormatter, and measuring gaps.
Almost every real application deals with dates and times — a blog post's publish date, a user's signup timestamp, "3 days ago," scheduling something for next week. Java's modern Date & Time API (from java.time, introduced in Java 8) makes this clean and pleasant. This lesson shows you the handful of classes you'll actually use — and steers you away from the messy old ones.
java.util.Date and Calendar. Avoid them — they're clunky, error-prone, and (surprisingly) mutable. Modern Java replaced them with the far better java.time package. Everything below uses the modern, recommended classes.The three classes you'll use most
| Class | Represents | Example |
|---|---|---|
LocalDate |
A date (no time) | 2026-09-17 |
LocalTime |
A time (no date) | 14:30:00 |
LocalDateTime |
Both together | 2026-09-17T14:30 |
Getting the current date and time
import java.time.*; LocalDate today = LocalDate.now(); // 2026-09-17 LocalTime now = LocalTime.now(); // 14:30:52.123 LocalDateTime dt = LocalDateTime.now(); // 2026-09-17T14:30:52
Creating a specific date
LocalDate birthday = LocalDate.of(2000, 5, 15); // year, month, day System.out.println(birthday.getDayOfWeek()); // MONDAY
Doing date maths (and immutability again)
Adding or subtracting time is beautifully readable. Note: these classes are immutable, so each operation returns a new date — just like Strings:
LocalDate today = LocalDate.now(); LocalDate nextWeek = today.plusDays(7); // 7 days from now LocalDate lastMonth = today.minusMonths(1); LocalDate deadline = today.plusWeeks(2).plusDays(3); // chainable! // today is unchanged — a new object was returned each time
Comparing dates
LocalDate d1 = LocalDate.of(2026, 1, 1); LocalDate d2 = LocalDate.of(2026, 12, 31); System.out.println(d1.isBefore(d2)); // true System.out.println(d1.isAfter(d2)); // false
Formatting dates for display
To show a date in a friendly format, use a DateTimeFormatter:
import java.time.format.DateTimeFormatter; LocalDate date = LocalDate.now(); DateTimeFormatter fmt = DateTimeFormatter.ofPattern("dd MMM yyyy"); System.out.println(date.format(fmt)); // 17 Sep 2026
The reverse — turning text into a date — uses LocalDate.parse(text, fmt). Together they cover reading dates in and printing them out nicely.
Measuring the gap between dates
import java.time.temporal.ChronoUnit; LocalDate start = LocalDate.of(2026, 1, 1); LocalDate end = LocalDate.of(2026, 9, 17); long days = ChronoUnit.DAYS.between(start, end); // 259
The java.time API is one of those parts of modern Java that's just genuinely nice to use — readable, safe, and immutable, which sidesteps a whole family of old date bugs. On this website, every blog post carries created/published/scheduled timestamps, and the scheduling feature that decides when a post goes live is all built on exactly these classes. Reach for LocalDate, LocalTime, and LocalDateTime — and leave the old Date/Calendar classes in the past where they belong.
Key takeaways
- Use the modern
java.timeAPI —LocalDate,LocalTime,LocalDateTime— not oldDate/Calendar. - Get current values with
.now(); build specific ones with.of(...). - Do date maths with
plusDays/minusMonths(immutable — returns new objects); compare withisBefore/isAfter. - Format for display with
DateTimeFormatter; measure gaps withChronoUnit.DAYS.between.
← Previous: Lesson 31 — File I/O in Java
Next: Lesson 33 — JVM, Memory & Garbage Collection →