Skip to content
AgileLogics
4 min read

Working with Dates and Times

Prerequisites: File I/O

The java.time package (since Java 8) replaced the old, error-prone Date/Calendar classes with immutable, clearly-named types:

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

LocalDate today = LocalDate.now();
LocalDate birthday = LocalDate.of(1990, 5, 15);

System.out.println(today.isAfter(birthday)); // true

DateTimeFormatter fmt = DateTimeFormatter.ofPattern("dd/MM/yyyy");
System.out.println(today.format(fmt));

LocalDate (date only), LocalTime (time only), and LocalDateTime (both) cover most everyday needs; ZonedDateTime adds a time zone when that matters.