Java Programs›Date & Time›Add Days and Months to a Date

Add Days and Months to a Date in Java

beginner·  Date & Time  ·  Date & Time

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.

Input
2026-01-31
Output
+30 days: 2026-03-02, +1 month: 2026-02-28

Java Program

Java
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

Invoice date: 2026-01-31 Plus 30 days: 2026-03-02 Plus 1 month: 2026-02-28 Plus 2 weeks: 2026-02-14 Minus 1 year: 2025-01-31 Original unchanged: 2026-01-31

Core Logic

Each plus or minus method works on the calendar, handling month lengths and leap years for you.

How It Works
  1. 1plusDays(30) crosses into March, because February 2026 has only 28 days.
  2. 2plusMonths(1) would land on 31 February, which doesn't exist, so it clamps to the last valid day, 28 February.
  3. 3plusWeeks(2) and minusYears(1) work the same way.
  4. 4The last line proves invoice itself never changed.
31 January plus 30 days is 2 March, but plus one month is 28 February — "a month" and "30 days" are different things.
💡

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

plusDays()plusMonths()minusYears()immutability

Approach 2: Using TemporalAdjusters

Java
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

Last day of month: 2026-03-31 First day of year: 2026-01-01 Next Friday: 2026-03-13 Next or same Monday: 2026-03-09

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.

How It Works
  1. 1with(lastDayOfMonth()) moves to the 31st of March.
  2. 2with(firstDayOfYear()) moves to 1 January.
  3. 3next(DayOfWeek.FRIDAY) finds the following Friday; nextOrSame keeps the date if it already matches.
9 March 2026 is a Monday, so 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

TemporalAdjusterswith()DayOfWeek

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.

Learn the Concept

Related Programs