Codectionary / Developer documentation / Java

Calendar Dates with LocalDate

LocalDate models a calendar date without time or time zone. Date arithmetic handles month lengths and leap years while returning immutable results. Save as Main.java and run with a Java 17+ JDK: java Main.java.

Syntax

LocalDate.parse("2024-02-28").plusDays(1)

Examples

Calendar Dates with LocalDate

Save as Main.java and run with a Java 17+ JDK: java Main.java.

import java.time.LocalDate;

public class Main {
  public static void main(String[] args) throws Exception {
    LocalDate start = LocalDate.parse("2024-02-28");
    System.out.println(start.plusDays(1));
    System.out.println(start.plusDays(2));
    System.out.println(start);
  }
}
  1. Expected output: 2024-02-29 2024-03-01 2024-02-28
  2. 2024 is a leap year, so February has 29 days. The original date remains unchanged.

Best practices

  • Use LocalDate for birthdays and calendar deadlines that do not identify an instant.
  • Use Instant or a zoned type when the time of day and offset matter.

Runtime & versions

Java Virtual Machine. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

Java Language Specification

Specifications & further reading