Library contents

Java LocalDate.parse and plusDays

A LocalDate is a calendar date without a time of day. Adding days handles month ends and leap years for you.

A simple example

LocalDate.parse and plusDays

  1. Start with 2024-01-31
  2. Do this Add one calendar day
  3. Result 2024-02-01
January ends on day 31. The next day starts a new month.

Syntax

Java syntax
LocalDate.parse(text).plusDays(1)
Run these examples in Java

Save one example at a time as Main.java. Use JDK 21.

java Main.java

Validate a date before adding a day

An office prints dates on receipts or billing records that may cross a month boundary. Use calendar-aware date handling and a consistent representation so the displayed date can be read back unambiguously.

First output line2024-03-01
Main.java
import java.time.LocalDate;
import java.time.format.DateTimeParseException;

class Main {
    public static void main(String[] args) throws Exception {
        for (String text : new String[]{"2024-02-29", "2023-02-29"}) {
            try {
                System.out.println(LocalDate.parse(text).plusDays(1));
            } catch (DateTimeParseException ex) {
                System.out.println("invalid date");
            }
        }
    }
}
Output
2024-03-01
invalid date

February 29 is valid in 2024 and invalid in 2023. Calendar days are appropriate for a due date that has no time-of-day component.

Advertisements

Return a borrowed microscope

A school lends a microscope two days before the end of February in a leap year. Parse the stated local calendar date and add the indicated number of days. Which date goes on the reminder?

Given28 February 2024 + 2 days
Worked result2024-03-01

Send a year-end reminder

A gallery schedules its reminder one day after the final day of the year. Parse the stated local calendar date and add the indicated number of days. Which date goes on the reminder?

Given31 December 2026 + 1 day
Worked result2027-01-01

What to watch for

Calendar days include weekends. A business-day schedule needs a separate rule for weekends and holidays.

Documentation for LocalDate.parse and plusDays