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, andZonedDateTime - Use date arithmetic (
plusDays/minusMonths, etc.) - Parse and format with
DateTimeFormatter - Express time/period as
DurationandPeriod
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
- Use old
java.util.Date/Calendar(not recommended) - Trying to handle date intervals with
Durationand time intervals withPeriod(roles are reversed) - Compare times in different regions without missing time zones
- Since the object is immutable, only
today.plusDays(1)is called and no result is received → meaningless - Store the local time as is in the DB → It will be a different time for users in other regions
Cleanup
java.timeis 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