Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Java reference

LocalDate.of()

Creates a date from a year, month and day.

Available since: Java 8

Syntax

static LocalDate of(int year, int month, int dayOfMonth)

Parameters

ParameterTypeDescription
yearintThe year.
monthint / MonthThe month from 1 to 12, or a constant such as Month.MARCH.
dayOfMonthintThe day of the month.

Return value

LocalDateA new date.

Examples

A date and its parts:

void main() {
    LocalDate date = LocalDate.of(2025, 3, 14);
    IO.println(date);
    IO.println(date.getMonth());
    IO.println(date.getDayOfYear());
    IO.println(date.isLeapYear());
}

Output:

2025-03-14
MARCH
73
false

A date that does not exist throws an exception:

void main() {
    try {
        LocalDate.of(2025, 2, 30);
    } catch (DateTimeException e) {
        IO.println(e.getMessage());
    }
}

Output:

Invalid date 'FEBRUARY 30'

Notes

Months start at 1, not 0 as in the old java.util.Calendar. Compare dates with isBefore(), isAfter(), isEqual() or equals().

See also

  • LocalDate.now() — Returns today's date from the computer's clock and time zone.
  • LocalDate.parse() — Parses a date from a string — in ISO format (2025-03-14) or with a pattern.
  • LocalDate.plusDays() — Returns a date the given number of days later. Related: minusDays(), plusWeeks(), plusMonths(), plusYears().

Related recipes

In the tutorial

  • Dates and times — The java.time package — LocalDate, LocalTime, LocalDateTime, date arithmetic, Period, Duration, ChronoUnit, time zones and Instant.

Thanks for reading!

Donate to the site — we work hard for you.