Java Dates in Depth
LocalTime holds a time of day, and Period holds a span of years, months, and days. Both are built from fixed numbers in this lesson.
A fixed time of day
LocalTime.of builds half past nine. Adding two hours shifts it to half past eleven.
import java.time.LocalTime;
public class Main {
public static void main(String[] args) {
LocalTime time = LocalTime.of(9, 30);
System.out.println(time);
System.out.println(time.plusHours(2));
}
}The span between two dates
Period.between measures from February 10, 2024 to May 15, 2026. The three parts print as separate numbers.
import java.time.LocalDate;
import java.time.Period;
public class Main {
public static void main(String[] args) {
LocalDate start = LocalDate.of(2024, 2, 10);
LocalDate end = LocalDate.of(2026, 5, 15);
Period span = Period.between(start, end);
System.out.println(span.getYears());
System.out.println(span.getMonths());
System.out.println(span.getDays());
}
}Shift a date by a Period
A span of two months and ten days shifts January 15, 2026 to March 25, 2026. The weekday line names the fixed start dateโs weekday.
import java.time.LocalDate;
import java.time.Period;
public class Main {
public static void main(String[] args) {
LocalDate date = LocalDate.of(2026, 1, 15);
Period span = Period.of(0, 2, 10);
System.out.println(date.plus(span));
System.out.println(date.getDayOfWeek());
}
}Order two dates
The earlier date is before the later one, and the later one is after the earlier one. The reversed check prints false.
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
LocalDate earlier = LocalDate.of(2026, 1, 15);
LocalDate later = LocalDate.of(2026, 3, 25);
System.out.println(earlier.isBefore(later));
System.out.println(later.isAfter(earlier));
System.out.println(earlier.isAfter(later));
}
}Example
import java.time.LocalDate;
import java.time.Period;
public class Main {
public static void main(String[] args) {
LocalDate start = LocalDate.of(2025, 3, 1);
LocalDate end = LocalDate.of(2026, 6, 20);
Period span = Period.between(start, end);
System.out.println(span.getYears());
System.out.println(span.getMonths());
System.out.println(span.getDays());
System.out.println(start.plus(span));
System.out.println(start.getDayOfWeek());
System.out.println(start.isBefore(end));
}
}Key points
- LocalTime.of builds a time of day from an hour and a minute.
- Period.between answers the span from one date to a later date.
- The getYears, getMonths, and getDays methods read the spanโs parts as numbers.
- Adding a Period to a date with plus shifts the date by the whole span.
- The getDayOfWeek method answers the weekday name of a fixed date.
- The isBefore and isAfter methods answer true or false about the order of two dates.
In this example, the span from March 1, 2025 to June 20, 2026 prints as 1 year, 3 months, and 19 days. Adding that span back to the start lands on the end date, the start dateโs weekday is Saturday, and the order check prints true.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
Which method answers the span between two dates?