LocalDate.now() / of() / getYear() / getMonth()
LocalDate is a class that represents a date (year, month, day) without time information (Java 8+). It is part of the modern date-time API that replaces the legacy java.util.Date, and its instances are immutable.
Syntax
// Get today's date. LocalDate today = LocalDate.now(); // Create a date with a specific year, month, and day. LocalDate date = LocalDate.of(year, month, day); // Get the year. date.getYear(); // Get the month as a number (1–12). date.getMonthValue(); // Get the day of the month (1–31). date.getDayOfMonth();
Method List
| Method | Description |
|---|---|
| LocalDate.now() | Returns today's date using the system default time zone. |
| LocalDate.of(year, month, day) | Creates a LocalDate for the specified year, month, and day. |
| getYear() | Returns the year as an int. |
| getMonthValue() | Returns the month as an int from 1 to 12. |
| getDayOfMonth() | Returns the day of the month as an int from 1 to 31. |
| getDayOfWeek() | Returns the day of the week as a DayOfWeek enum value (MONDAY–SUNDAY). |
| plusDays(long n) | Returns a new LocalDate with the specified number of days added. |
| isBefore(LocalDate) | Returns a boolean indicating whether this date is before the specified date. |
Sample Code
import java.time.LocalDate; import java.time.DayOfWeek; // Get today's date. LocalDate today = LocalDate.now(); System.out.println(today); // e.g., prints "2025-04-01" // Create a specific date. LocalDate date = LocalDate.of(2025, 4, 1); System.out.println(date.getYear()); // prints "2025" System.out.println(date.getMonthValue()); // prints "4" System.out.println(date.getDayOfMonth()); // prints "1" // Get the day of the week. DayOfWeek dow = date.getDayOfWeek(); System.out.println(dow); // prints "TUESDAY" // Add days (returns a new object because LocalDate is immutable). LocalDate next = today.plusDays(7); System.out.println(next); // prints the date one week from today // Compare dates. LocalDate past = LocalDate.of(2020, 1, 1); System.out.println(past.isBefore(today)); // prints "true" System.out.println(past.isAfter(today)); // prints "false"
Notes
Because LocalDate is immutable, calling methods like plusDays() or minusMonths() does not modify the original object. To use the updated date, assign the returned new object to a variable.
If you need to account for time zones, see ZonedDateTime. To work with both date and time, see LocalDateTime.
If you find any errors or copyright issues, please contact us.