Signup/Sign In
LAST UPDATED: NOVEMBER 27, 2020

Java LocalDate plusMonths() Method

Java plusMonths() method is used to add the specified number of months to a date. It adds the months and returns a new LocalDate. For example, 2009-10-10 plus one month would result in 2009-11-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 plusMonths(long monthsToAdd)

Parameters:

It takes a parameter of long type.

Returns:

It returns a local-date after adding the specified months to a date.

Time for an Example:

Let's take an example to add months to a date. Here, we are adding 2 months by using the plusMonths() 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.plusMonths(2);
		System.out.println("New date : "+localDate);
	}
}


2016-10-21
New date : 2016-12-21

Time for another Example:

Let's take another example to understand the plusMonths() method. Here, we are adding 2 months to 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.plusMonths(2);
		System.out.println("New date : "+localDate);
	}
}


2020-06-17
New date : 2020-08-17

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.