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
- Start with 2024-01-31
- Do this Add one calendar day
- Result 2024-02-01
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.javaValidate 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.
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");
}
}
}
}
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.
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?
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?
What to watch for
Calendar days include weekends. A business-day schedule needs a separate rule for weekends and holidays.