Date and Time

Java subtract dates from given date

public final class LocalDate extends Object

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.

public LocalDate minusMonths(long monthsToSubtract)

Returns a copy of this LocalDate with the specified number of months subtracted.

This method subtracts the specified amount from the months field in three steps:

  1. Subtract the input months from the month-of-year field
  2. Check if the resulting date would be invalid
  3. Adjust the day-of-month to the last valid day if necessary

public LocalDate minusWeeks(long weeksToSubtract)

Returns a copy of this LocalDate with the specified number of weeks subtracted. This method subtracts the specified amount in weeks 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-07 minus one week would result in 2008-12-31.

public LocalDate minusYears(long yearsToSubtract)

Returns a copy of this LocalDate with the specified number of years subtracted. This method subtracts the specified amount from the years field in three steps:

  1. Subtract the input years from the year field
  2. Check if the resulting date would be invalid
  3. Adjust the day-of-month to the last valid day if necessary

For example, 2008-02-29 (leap year) minus one year would result in the invalid date 2007-02-29 (standard year). Instead of returning an invalid result, the last valid day of the month, 2007-02-28, is selected instead.

package com.candidjava.datetime;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Calendar;

public class JavaSubtractDate {
public static void main(String[] args) {
	//Using LocalDateTime Class
			LocalDate date=LocalDate.now();
			System.out.println("Using LocalDate time class:");
			System.out.println("Current Date: "+date);
		    date=date.minusDays(7);
			System.out.println("Date after decrementing days:"+date);
			date=date.minusMonths(5);
			System.out.println("Date after decrementing months:"+date);
			date=date.minusYears(2);
			System.out.println("Date after decrementing years:"+date);
			date=date.minusWeeks(1);
			System.out.println("Date after decrementing weeks:"+date);
			System.out.println("------------------------------------------");
			LocalDate newdate=LocalDate.of(2020, 8, 27);
			System.out.println("Date:"+newdate);
			newdate=newdate.minusDays(2).minusMonths(5).minusYears(1);
			System.out.println("Decremented Date:"+newdate);
			System.out.println("------------------------------------------");
			//Using Calendar Class
			SimpleDateFormat dateFormat=new SimpleDateFormat("dd-MM-yyyy");
			Calendar calendar=Calendar.getInstance();
			System.out.println("Using Calendar Class:");
			System.out.println("Current Date:"+dateFormat.format(calendar.getTime()));
			calendar.add(Calendar.DAY_OF_MONTH, -2);
			calendar.add(Calendar.MONTH, -1);
			calendar.add(Calendar.YEAR, -1);
			String incrementedDate=dateFormat.format(calendar.getTime());
			System.out.println("Decremented Date:"+incrementedDate);
			System.out.println("------------------------------------------");

}
}

OUTPUT

Using LocalDate time class:
Current Date: 2020-11-18
Date after decrementing days:2020-11-11
Date after decrementing months:2020-06-11
Date after decrementing years:2018-06-11
Date after decrementing weeks:2018-06-04
------------------------------------------
Date:2020-08-27
Decremented Date:2019-03-25
------------------------------------------
Using Calendar Class:
Current Date:18-11-2020
Decremented Date:16-10-2019
------------------------------------------