← Back to Java series
🔧
Modern Java
Modern Java · Prerequisite: previous lecture

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`.

Javamodernjava.timeLocalDateZonedDateTime
Duration
~1.5-2 hours
Level
📊 Intermediate-Advanced
Prerequisite
🎯 Previous lecture or equivalent knowledge
OUTCOME
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

TypeRepresentsExample
`LocalDate`date2026-05-16
`LocalTime`time of day09:30
`LocalDateTime`date + time2026-05-16T09:30
`ZonedDateTime`above + zone2026-05-16T09:30+09:00[Asia/Seoul]

2) Construction

java
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

java
LocalDate tomorrow = today.plusDays(1);
LocalDate lastWeek = today.minusWeeks(1);
boolean before = today.isBefore(xmas);

4) Format / parse

java
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`

java
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`

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**

text
2026-05-16
09:30
2026-05-16T09:30

Example 2 — `Arithmetic.java`

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**

text
2026-05-23
2026-03-16
2026-05-01

Example 3 — `FormatParse.java`

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**

text
2026/05/16
2026-12-25

Example 4 — `DurationPeriod.java`

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**

text
hours: 1, minutes: 30
months: 4, days: 15

Common Mistakes

  1. Mixing `java.util.Date` with `java.time` — convert via `Date.from(instant)` / `instant.toDate()`
  2. Forgetting that `LocalDateTime` has **no** time zone — use `ZonedDateTime` for cross-zone work
  3. Using `Duration` for date-based intervals or `Period` for time-based — they're different concepts
  4. Hard-coding date patterns instead of using `DateTimeFormatter`
  5. 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

text
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

text
Saturday, 16 May 2026

## Solutions After trying it yourself, compare with [`answer/`](./answer/).

Solution code (homework/answer/)

answer/Homework01.java

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

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

bash
cd 05_modern/20_datetime/src
javac FormatParse.java
java FormatParse

Next Lecture

[21_Maven_Gradle](../21_Maven_Gradle/) — build tools and dependency management.

Example code / lecture materials

All lecture materials and example code are openly available on GitHub.

View on GitHub ↗