Add Days and Months to a Date in Java
Problem
LocalDate is immutable: methods like plusDays never change the original date, they return a new LocalDate with the adjustment applied.
Starting from an invoice dated 31 January 2026, compute dates 30 days, 1 month and 2 weeks later, and 1 year earlier.
Java Program
import java.time.LocalDate;
public class AddToDate {
public static void main(String[] args) {
LocalDate invoice = LocalDate.of(2026, 1, 31);
System.out.println("Invoice date: " + invoice);
System.out.println("Plus 30 days: " + invoice.plusDays(30));
System.out.println("Plus 1 month: " + invoice.plusMonths(1)); // no 31 Feb, so it clamps to 28 Feb
System.out.println("Plus 2 weeks: " + invoice.plusWeeks(2));
System.out.println("Minus 1 year: " + invoice.minusYears(1));
System.out.println("Original unchanged: " + invoice); // LocalDate is immutable
}
}Output
Core Logic
Each plus or minus method works on the calendar, handling month lengths and leap years for you.
- 1
plusDays(30)crosses into March, because February 2026 has only 28 days. - 2
plusMonths(1)would land on 31 February, which doesn't exist, so it clamps to the last valid day, 28 February. - 3
plusWeeks(2)andminusYears(1)work the same way. - 4The last line proves
invoiceitself never changed.
Key Point: Writing invoice.plusDays(30); on its own line does nothing useful — the result must be assigned, because the original object is immutable.
Key Concepts
Approach 2: Using TemporalAdjusters
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
public class AddToDateAdjusters {
public static void main(String[] args) {
LocalDate today = LocalDate.of(2026, 3, 9); // a Monday
System.out.println("Last day of month: " + today.with(TemporalAdjusters.lastDayOfMonth()));
System.out.println("First day of year: " + today.with(TemporalAdjusters.firstDayOfYear()));
System.out.println("Next Friday: " + today.with(TemporalAdjusters.next(DayOfWeek.FRIDAY)));
System.out.println("Next or same Monday: " + today.with(TemporalAdjusters.nextOrSame(DayOfWeek.MONDAY)));
}
}
Output
Core Logic
Instead of adding a fixed amount, TemporalAdjusters jump to a calendar landmark such as the end of the month or the next Friday.
- 1
with(lastDayOfMonth())moves to the 31st of March. - 2
with(firstDayOfYear())moves to 1 January. - 3
next(DayOfWeek.FRIDAY)finds the following Friday;nextOrSamekeeps the date if it already matches.
nextOrSame(MONDAY) returns the same date while next(FRIDAY) returns 13 March.Key Point: Adjusters express business rules such as "due on the last day of the month" directly, without fragile day arithmetic.
Key Concepts
Interview Questions
What's the difference between plusMonths(1) and plusDays(30)?
plusMonths follows the calendar and keeps the day of month where possible, while plusDays(30) always adds exactly 30 days.
How do you skip weekends when adding days?
Add one day at a time and skip Saturday and Sunday by checking getDayOfWeek(), or use a custom TemporalAdjuster.