Java + Spring Boot track

Lesson 20 · Part 5: Modern Java

Date and Time

The standard API that handles date and time has been extensively reorganized with the java.time package starting from JDK 8. Equipped with immutable objects, clear…

Part 5: Modern Java3 min readFree lesson

The standard API that handles date and time has been extensively reorganized with the java.time package starting from JDK 8. Equipped with immutable objects, clear semantics, and time zone handling, it is overwhelmingly easier to use than the previous Date/Calendar.

Learning Objectives

  • Know the difference between LocalDate, LocalTime, LocalDateTime, and ZonedDateTime
  • Use date arithmetic (plusDays / minusMonths, etc.)
  • Parse and format with DateTimeFormatter
  • Express time/period as Duration and Period

Key concepts

1) 4 types

Type expression Yes
LocalDate date 2026-05-16
LocalTime time 09:30
LocalDateTime Date+Time 2026-05-16T09:30
ZonedDateTime above + time zone 2026-05-16T09:30+09:00[Asia/Seoul]

2) Create

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);

java.time objects are all immutable — their methods return a new instance.

4) Formatting

DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy.MM.dd HH:mm");
String s = LocalDateTime.now().format(fmt);
LocalDate parsed = LocalDate.parse("2026-05-16");

5) Duration vs Period

class Unit Use
Duration seconds/nanoseconds Interval between times
Period Year·Month·Day Interval between dates

See as example

Example 1 — DateBasics.java: Generation and Arithmetic

import java.time.LocalDate;

public class DateBasics {
    public static void main(String[] args) {
        LocalDate today = LocalDate.of(2026, 5, 16);
        LocalDate xmas = LocalDate.of(2026, 12, 25);
        System.out.println("Today:" + today);
        System.out.println("Christmas:" + xmas);
        System.out.println("Tomorrow:" + today.plusDays(1));
        System.out.println("1 month ago:" + today.minusMonths(1));
        System.out.println("Before Christmas?" + today.isBefore(xmas));
    }
}

Execution result

Today: 2026-05-16
Christmas: 2026-12-25
Tomorrow: 2026-05-17
1 month ago: 2026-04-16
Before Christmas? true

Note: ISO-8601 (yyyy-MM-dd) is the default representation.

Example 2 — TimeBasics.java: Time and time zone

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class TimeBasics {
    public static void main(String[] args) {
        LocalDateTime ldt = LocalDateTime.of(2026, 5, 16, 9, 30);
        System.out.println("Local:" + ldt);

        ZonedDateTime seoul = ldt.atZone(ZoneId.of("Asia/Seoul"));
        ZonedDateTime ny = seoul.withZoneSameInstant(ZoneId.of("America/New_York"));
        System.out.println("Seoul:" + seoul);
        System.out.println("New York:" + ny);
    }
}

Execution result

Local: 2026-05-16T09:30
Seoul: 2026-05-16T09:30+09:00[Asia/Seoul]
New York: 2026-05-15T20:30-04:00[America/New_York]

Note: withZoneSameInstant converts same moment to a different time zone representation.

Example 3 — Formatter.java: Format/Parsing

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Formatter {
    public static void main(String[] args) {
        LocalDateTime dt = LocalDateTime.of(2026, 5, 16, 9, 30, 45);
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy.MM.dd HH:mm:ss");
        System.out.println(dt.format(fmt));

        LocalDateTime parsed = LocalDateTime.parse("2026-05-16T09:30:45");
        System.out.println(parsed);
    }
}

Execution result

2026.05.16 09:30:45
2026-05-16T09:30:45

Memo: The ISO standard format is parse without a separate formatter.

Example 4 — DurationAndPeriod.java: Period calculation

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.Period;

public class DurationAndPeriod {
    public static void main(String[] args) {
        LocalTime a = LocalTime.of(9, 0);
        LocalTime b = LocalTime.of(17, 30);
        Duration work = Duration.between(a, b);
        System.out.println("Working minutes:" + work.toMinutes());

        LocalDate birth = LocalDate.of(2005, 3, 1);
        LocalDate now = LocalDate.of(2026, 5, 16);
        Period age = Period.between(birth, now);
        System.out.printf("Age: %d years %d months %d days %n",
            age.getYears(), age.getMonths(), age.getDays());
    }
}

Execution result

Minutes worked: 510
Age: 21 years, 2 months, 15 days

Memo: Use Duration for the time interval and Period for the date interval.

Common Mistakes

  1. Use old java.util.Date / Calendar (not recommended)
  2. Trying to handle date intervals with Duration and time intervals with Period (roles are reversed)
  3. Compare times in different regions without missing time zones
  4. Since the object is immutable, only today.plusDays(1) is called and no result is received → meaningless
  5. Store the local time as is in the DB → It will be a different time for users in other regions

Cleanup

  • java.time is an immutable, clear, and rich modern API.
  • Select and use four core types according to the situation
  • Formatting is DateTimeFormatter
  • The period is clearly divided into Duration / Period

Try it yourself

cd 05_Modern_Java/20_Date_Time/src
javac DateBasics.java
java DateBasics

Example files

The runnable files used in this lesson. Copy them into your own project and experiment.

src/DateBasics.javaJava
import java.time.LocalDate;

public class DateBasics {
    public static void main(String[] args) {
        LocalDate today = LocalDate.of(2026, 5, 16);
        LocalDate xmas = LocalDate.of(2026, 12, 25);
        System.out.println("Today:" + today);
        System.out.println("Christmas:" + xmas);
        System.out.println("Tomorrow:" + today.plusDays(1));
        System.out.println("1 month ago:" + today.minusMonths(1));
        System.out.println("Before Christmas?" + today.isBefore(xmas));
    }
}
src/DurationAndPeriod.javaJava
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.Period;

public class DurationAndPeriod {
    public static void main(String[] args) {
        LocalTime a = LocalTime.of(9, 0);
        LocalTime b = LocalTime.of(17, 30);
        Duration work = Duration.between(a, b);
        System.out.println("Working minutes:" + work.toMinutes());

        LocalDate birth = LocalDate.of(2005, 3, 1);
        LocalDate now = LocalDate.of(2026, 5, 16);
        Period age = Period.between(birth, now);
        System.out.printf("Age: %d years %d months %d days %n",
            age.getYears(), age.getMonths(), age.getDays());
    }
}
src/Formatter.javaJava
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Formatter {
    public static void main(String[] args) {
        LocalDateTime dt = LocalDateTime.of(2026, 5, 16, 9, 30, 45);
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy.MM.dd HH:mm:ss");
        System.out.println(dt.format(fmt));

        LocalDateTime parsed = LocalDateTime.parse("2026-05-16T09:30:45");
        System.out.println(parsed);
    }
}
src/TimeBasics.javaJava
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class TimeBasics {
    public static void main(String[] args) {
        LocalDateTime ldt = LocalDateTime.of(2026, 5, 16, 9, 30);
        System.out.println("Local:" + ldt);

        ZonedDateTime seoul = ldt.atZone(ZoneId.of("Asia/Seoul"));
        ZonedDateTime ny = seoul.withZoneSameInstant(ZoneId.of("America/New_York"));
        System.out.println("Seoul:" + seoul);
        System.out.println("New York:" + ny);
    }
}

Assignment

Problem 1 — Calculating D-Day from today

  • File name: Homework01.java
  • Core concepts: LocalDate, Period, Duration

Requirements

  • Base date 2026-05-16, target date 2026-12-25 (Christmas).
  • Output the difference between two dates in “X days” format as Period.between.

Expected output

D-223 days

Hint

  • ChronoUnit.DAYS.between(a, b) is the most intuitive.

Problem 2 — Attendance Format

  • File name: Homework02.java
  • Core concepts: LocalDateTime, DateTimeFormatter

Requirements

  • Create a visual 2026-05-16 09:00 and output it in the following two formats.
  • Format A: Day m, month M, year yyyy a h hour m minute (e.g. May 16, 2026 9:00 AM)
  • Format B: yyyy-MM-dd HH:mm

Expected output

May 16, 2026 9:00 AM
2026-05-16 09:00

Check the correct answer

After solving it yourself, compare it with the reference solution below.

Show the reference solution

Try the assignment yourself first — comparing your attempt with a working solution is where most of the learning happens.

Homework01.javaJava
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

/** D-Day calculation.*/
public class Homework01 {
    public static void main(String[] args) {
        LocalDate base = LocalDate.of(2026, 5, 16);
        LocalDate target = LocalDate.of(2026, 12, 25);
        long days = ChronoUnit.DAYS.between(base, target);
        System.out.println("D-" + days + "work");
    }
}
Homework02.javaJava
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

/** Two formats for work time.*/
public class Homework02 {
    public static void main(String[] args) {
        LocalDateTime dt = LocalDateTime.of(2026, 5, 16, 9, 0);
        DateTimeFormatter a = DateTimeFormatter.ofPattern("Day m, month M, year yyyy a h hour m minute", Locale.KOREAN);
        DateTimeFormatter b = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        System.out.println(dt.format(a));
        System.out.println(dt.format(b));
    }
}

Learn this lesson with a mentor

Work through the Java + Spring Boot track on your own, or join a guided cohort at our Pitampura campus and online.

Call NowEnroll Now