Signup/Sign In

Java LocalDate minusMonth() Method

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

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

Syntax

public LocalDate minusMonths(long monthsToSubtract)

Parameters:

It takes a parameter of long type.

Returns:

It returns a local-date after subtracting the specified months from a date.

Time for an Example:

Let's take an example to subtract months from a date. Here, we are subtracting 2 months by using the minusMonths() method that returns a new local-date. See the below example.

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.minusMonths(2);
		System.out.println("New date : "+localDate);
	}
}


2016-10-21
New date : 2016-08-21

Time for another Example:

Let's take another example to understand the minusMonths() method. Here, we are subtracting 2 months from the current date. See the example below.

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


2020-06-10
New date : 2020-04-10

Live Example:

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



About the author:
I am a 3rd-year Computer Science Engineering student at Vellore Institute of Technology. I like to play around with new technologies and love to code.