Summary - Date/time API
- Java course
- Advanced
- Lesson 24/82
- 5 min
Combine LocalDate, Period and DateTimeFormatter to write an age calculator.
Summary - Date/time API
In this group you covered the core classes of the java.time package. LocalDate holds a date, LocalTime a time, LocalDateTime both together. Duration gives hour-based, Period day-based lengths. Instant holds a UTC moment, ZonedDateTime a regional time.
DateTimeFormatter turns dates and times into strings and reads them back. These classes are immutable, which makes math safe; they replace the old Date class in a modern way.
Practice Age calculation with Period.between
Create a birth date with LocalDate.of(2000, 5, 15) and today with LocalDate.of(2024, 5, 15). Print the age with Period.between(birth, today).getYears() via System.out.println.
Starter code
import java.time.LocalDate;
import java.time.Period;
// Create birth and today dates
// Take the difference with Period.between
// Print the age with getYearsSample solution
import java.time.LocalDate;
import java.time.Period;
public class BirthdayCalc {
public static void main(String[] args) {
LocalDate birth = LocalDate.of(2000, 5, 15);
LocalDate today = LocalDate.of(2024, 5, 15);
System.out.println(Period.between(birth, today).getYears());
}
}Expected output
24
Which class when?
Choosing the right class depends on the question. Use LocalDate for date only, LocalTime for time only, LocalDateTime when both are needed. Pick Instant for global time and ZonedDateTime for regional display.
For lengths, use Period for dates and Duration for times. For display, DateTimeFormatter always works. Avoid the legacy Date and Calendar classes; the java.time package offers a modern, consistent alternative.
Practice Format and compute a meeting duration
Create start with LocalDateTime.of(2024, 1, 15, 10, 0) and end with LocalDateTime.of(2024, 1, 15, 12, 30). Get the minutes with Duration.between(start, end).toMinutes(). Format the start with DateTimeFormatter.ofPattern("HH:mm"). Print formatted + " - " + minutes + " min" via System.out.println.
Starter code
import java.time.LocalDateTime;
import java.time.Duration;
import java.time.format.DateTimeFormatter;
// Create start and end
// Get the minutes with Duration.between
// Format start with HH:mm and printSample solution
import java.time.LocalDateTime;
import java.time.Duration;
import java.time.format.DateTimeFormatter;
public class MeetingDuration {
public static void main(String[] args) {
LocalDateTime start = LocalDateTime.of(2024, 1, 15, 10, 0);
LocalDateTime end = LocalDateTime.of(2024, 1, 15, 12, 30);
long minutes = Duration.between(start, end).toMinutes();
DateTimeFormatter f = DateTimeFormatter.ofPattern("HH:mm");
System.out.println(start.format(f) + " - " + minutes + " min");
}
}Expected output
10:00 - 150 min
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which class holds a year-month-day based date range?
- Duration
- Instant
- LocalTime
- Period Correct answer
Explanation: Period is year-month-day based.
2 Match each class with its behavior
| Term | Meaning |
|---|---|
LocalDate |
Date |
LocalTime |
Time |
Duration |
Hour range |
Instant |
UTC moment |