Sub topic 3
Working with Calendar Dates Using LocalDate
A LocalDate is a Java object that holds a calendar date: a year, a month, and a day. It has no time of day and no time zone. Use it for things like birthdays, holidays, and due dates, where only the day matters.
LocalDate lives in the java.time package. Like every java.time type, it is immutable, which means it never changes after you create it. Methods that seem to change a date actually return a new one.
Creating a LocalDate
You can create a date in three common ways:
LocalDate.of(year, month, day)builds a date from numbers. You can also pass aMonthconstant such asMonth.MARCHfor the month.LocalDate.parse("2024-12-25")reads a date from text in the standardyyyy-MM-ddformat.LocalDate.now()gives today's date from your computer's clock.
Deriving new dates
Methods that start with plus and minus return a new date. Each call returns a LocalDate, so you can chain several calls together.
import java.time.LocalDate;
import java.time.Month;
public class Dates {
public static void main(String[] args) {
LocalDate release = LocalDate.of(2014, Month.MARCH, 18);
LocalDate later = release.plusMonths(1).plusDays(10);
System.out.println(release); // 2014-03-18
System.out.println(later); // 2014-04-28
System.out.println(later.getDayOfWeek()); // MONDAY
}
}The chain runs from left to right. March 18 plus one month gives April 18, and adding ten days gives April 28.
Reading and comparing dates
Getter methods, which are methods that return a value without changing anything, let you read each part of a date. Other methods compare two dates.
import java.time.LocalDate;
public class DateInfo {
public static void main(String[] args) {
LocalDate leapDay = LocalDate.of(2024, 2, 29);
System.out.println(leapDay.getYear()); // 2024
System.out.println(leapDay.getMonthValue()); // 2
System.out.println(leapDay.getMonth()); // FEBRUARY
System.out.println(leapDay.getDayOfMonth()); // 29
System.out.println(leapDay.getDayOfYear()); // 60
System.out.println(leapDay.isLeapYear()); // true
System.out.println(leapDay.lengthOfMonth()); // 29
LocalDate nextYear = leapDay.plusYears(1);
System.out.println(nextYear); // 2025-02-28
LocalDate christmas = LocalDate.parse("2024-12-25");
System.out.println(leapDay.isBefore(christmas)); // true
System.out.println(leapDay.isAfter(christmas)); // false
System.out.println(christmas.equals(LocalDate.of(2024, 12, 25))); // true
}
}Look at plusYears(1). There is no February 29 in 2025, so Java moves the result back to the last valid day, February 28. Adding months behaves the same way.
Common methods at a glance
| Task | Method | Example result |
|---|---|---|
| Add days | `plusDays(n)` | new date n days later |
| Subtract weeks | `minusWeeks(n)` | new date n weeks earlier |
| Change one field | `withDayOfMonth(1)` | first day of the same month |
| Compare | `isBefore(other)` | `true` or `false` |
| Weekday | `getDayOfWeek()` | `MONDAY` |
Exercise
What does LocalDate.of(2024, 1, 31).plusMonths(1) print, and why?
Show answer
It prints 2024-02-29. February 2024 has no 31st day, so Java moves the result back to the last valid day of that month. 2024 is a leap year, so that day is the 29th.
Exercise
Write one line that creates the date for 100 days after 2024-06-01 and prints it.
Show answer
System.out.println(LocalDate.of(2024, 6, 1).plusDays(100)); prints 2024-09-09.