Java LocalDate minusDays() Method

Java minusDays() method is used to minus the specified number of days from a date. It subtracts the days and returns a new LocalDate. For example, 2009-01-10 minus one day would result in 2009-01-09.

It takes a long type argument that represents the number of days and returns a new LocalDate. The syntax of the method is given below.

Syntax

public LocalDate minusDays(long daysToSubtract)

Parameters:

It takes a parameter of long type.

Returns:

It returns a localdate after subtracting the days.

Time for an Example:

Let's take an example to subtract days from a date. Here, we are subtracting 2 days from a date by using the minusDays() method and we get a new date as a result.

import java.time.LocalDate; 
public class DateDemo {
	
	public static void main(String[] args){  
		
		LocalDate localDate = LocalDate.of(2016, 10, 21);
		System.out.println(localDate);
		localDate = localDate.minusDays(2);
		System.out.println("New date : "+localDate);
	}
}


2016-10-21
New date : 2016-10-19

Time for another Example:

Let's take another example to subtract the specified days from a date. Here, we are subtracting 2 days from current date. we used now() method to get current date and minusDays() method to get new date after subtracting the date.

import java.time.LocalDate; 
public class DateDemo {
	
	public static void main(String[] args){  
		
		LocalDate localDate = LocalDate.now();
		System.out.println(localDate);
		localDate = localDate.minusDays(2);
		System.out.println("New date : "+localDate);
	}
}


2020-06-10
New date : 2020-06-08

Live Example:

Try with a live example, execute the code instantly with our powerful Online Java Compiler.