20. Dates and Times (java.time)
Java's standard date/time API was overhauled in JDK 8 into the `java.time` package — immutable, semantically clear, with proper time-zone support. Far easier to use than the legacy `Date` / `Calendar`.
What you'll learn
- 1Distinguish `LocalDate`, `LocalTime`, `LocalDateTime`, `ZonedDateTime`
- 2Do date arithmetic with `plusDays` / `minusMonths`
- 3Parse and format with `DateTimeFormatter`
- 4Express durations / periods with `Duration` and `Period`
Overview
Java's standard date/time API was overhauled in JDK 8 into the `java.time` package — immutable, semantically clear, with proper time-zone support. Far easier to use than the legacy `Date` / `Calendar`.
Core Concepts
1) Four core types
| Type | Represents | Example |
|---|---|---|
| `LocalDate` | date | 2026-05-16 |
| `LocalTime` | time of day | 09:30 |
| `LocalDateTime` | date + time | 2026-05-16T09:30 |
| `ZonedDateTime` | above + zone | 2026-05-16T09:30+09:00[Asia/Seoul] |
2) Construction
import java.time.*;
LocalDate today = LocalDate.now();
LocalDate xmas = LocalDate.of(2026, 12, 25);
LocalDateTime dt = LocalDateTime.now();
ZonedDateTime seoul = ZonedDateTime.now(ZoneId.of("Asia/Seoul"));3) Arithmetic
LocalDate tomorrow = today.plusDays(1);
LocalDate lastWeek = today.minusWeeks(1);
boolean before = today.isBefore(xmas);4) Format / parse
import java.time.format.DateTimeFormatter;
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy/MM/dd");
String s = today.format(fmt);
LocalDate back = LocalDate.parse("2026/05/16", fmt);5) `Duration` and `Period`
Duration d = Duration.ofMinutes(30); // time-based
Period p = Period.between(today, xmas); // date-based (years, months, days)
System.out.println(p.getMonths());Examples
Example 1 — `Basics.java`
import java.time.*;
public class Basics {
public static void main(String[] args) {
LocalDate today = LocalDate.of(2026, 5, 16);
LocalTime now = LocalTime.of(9, 30);
LocalDateTime dt = LocalDateTime.of(today, now);
System.out.println(today);
System.out.println(now);
System.out.println(dt);
}
}**Output**
2026-05-16
09:30
2026-05-16T09:30Example 2 — `Arithmetic.java`
import java.time.LocalDate;
public class Arithmetic {
public static void main(String[] args) {
LocalDate today = LocalDate.of(2026, 5, 16);
System.out.println(today.plusDays(7));
System.out.println(today.minusMonths(2));
System.out.println(today.withDayOfMonth(1));
}
}**Output**
2026-05-23
2026-03-16
2026-05-01Example 3 — `FormatParse.java`
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class FormatParse {
public static void main(String[] args) {
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDate d = LocalDate.of(2026, 5, 16);
System.out.println(d.format(fmt));
LocalDate parsed = LocalDate.parse("2026/12/25", fmt);
System.out.println(parsed);
}
}**Output**
2026/05/16
2026-12-25Example 4 — `DurationPeriod.java`
import java.time.*;
public class DurationPeriod {
public static void main(String[] args) {
Duration d = Duration.ofMinutes(90);
System.out.println("hours: " + d.toHours() + ", minutes: " + d.toMinutesPart());
Period p = Period.between(LocalDate.of(2026, 1, 1), LocalDate.of(2026, 5, 16));
System.out.println("months: " + p.getMonths() + ", days: " + p.getDays());
}
}**Output**
hours: 1, minutes: 30
months: 4, days: 15Common Mistakes
- Mixing `java.util.Date` with `java.time` — convert via `Date.from(instant)` / `instant.toDate()`
- Forgetting that `LocalDateTime` has **no** time zone — use `ZonedDateTime` for cross-zone work
- Using `Duration` for date-based intervals or `Period` for time-based — they're different concepts
- Hard-coding date patterns instead of using `DateTimeFormatter`
- Mutating dates — every `plus...` returns a **new** instance
Summary
- `java.time` types are immutable
- `LocalDate` / `LocalTime` / `LocalDateTime` / `ZonedDateTime` cover most cases
- `Duration` for time-based, `Period` for date-based intervals
Practice
# Practice - 20. Dates and Times
## Exercise 1 — Days until Christmas
- File: `Homework01.java`
- Key concepts: `Period`, `LocalDate`
Requirements
- Given today = 2026-05-16 and Christmas = 2026-12-25, print months and days between.
Expected output
7 months 9 days## Exercise 2 — Custom format
- File: `Homework02.java`
- Key concepts: `DateTimeFormatter`
Requirements
- Format today as `Saturday, 16 May 2026` using a `DateTimeFormatter`.
Expected output
Saturday, 16 May 2026## Solutions After trying it yourself, compare with [`answer/`](./answer/).
Solution code (homework/answer/)
answer/Homework01.java
import java.time.LocalDate;
import java.time.Period;
/** Days until Christmas. */
public class Homework01 {
public static void main(String[] args) {
Period p = Period.between(LocalDate.of(2026, 5, 16), LocalDate.of(2026, 12, 25));
System.out.println(p.getMonths() + " months " + p.getDays() + " days");
}
}
answer/Homework02.java
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
/** Custom format. */
public class Homework02 {
public static void main(String[] args) {
var fmt = DateTimeFormatter.ofPattern("EEEE, d MMMM yyyy", Locale.ENGLISH);
System.out.println(LocalDate.of(2026, 5, 16).format(fmt));
}
}
Try It Yourself
cd 05_modern/20_datetime/src
javac FormatParse.java
java FormatParseNext Lecture
[21_Maven_Gradle](../21_Maven_Gradle/) — build tools and dependency management.
All lecture materials and example code are openly available on GitHub.
View on GitHub ↗