Date and Time

How to Add or Subtract Dates using LocalDate in Java

Table of Contents

Program

import java.time.LocalDate;
public class DateLocalDate 
{
	public static void main(String[] args) 
	{
		LocalDate date = LocalDate.now();
		LocalDate yesterday = date.minusDays(1);
		LocalDate tomorrow = yesterday.plusDays(2);
		System.out.println("Today date: " + date);
		System.out.println("Yesterday date: " + yesterday);
		System.out.println("Tommorow date: " + tomorrow);
	}
}

Output

Today date: 2021-04-11
Yesterday date: 2021-04-10
Tommorow date: 2021-04-12

Description

public LocalDate minusDays(long daysToSubtract)

Returns a copy of this LocalDate with the specified number of days subtracted.
This method subtracts the specified amount from the days field decrementing the month and year fields as necessary to ensure the result remains valid. The result is only invalid if the maximum/minimum year is exceeded.

Returns a copy of this LocalDate with the specified number of days subtracted.
This method subtracts the specified amount from the days field decrementing the month and year fields as necessary to ensure the result remains valid. The result is only invalid if the maximum/minimum year is exceeded.
For example, 2009-01-01 minus one day would result in 2008-12-31.
This instance is immutable and unaffected by this method call.

Parameters:

daysToSubtract – the days to subtract, may be negative

Returns:

a LocalDate based on this date with the days subtracted, not null

Throws:

DateTimeException – if the result exceeds the supported date range

public LocalDate plusDays(long daysToAdd)

Returns a copy of this LocalDate with the specified number of days added.
This method adds the specified amount to the day’s field incrementing the month and year fields as necessary to ensure the result remains valid. The result is only invalid if the maximum/minimum year is exceeded.|

For example, 2008-12-31 plus one days would result in 2009-01-01.

This instance is immutable and unaffected by this method call.

Parameters:

daysToAdd – the days to add, may be negative

Returns:

a LocalDate based on this date with the days added, not null

Throws:

DateTimeException – if the result exceeds the supported date range