Javaexercise.com

Java 8 LocalDate plusMonths() Method

Java plusMonths() method is used to add months to a date. It belongs to LocalDate class. This method is included in Java 8 version with DataTime API. We can use it to increase a date by adding months.

For example, 2020-01-01 plus one month will result in 2020-02-01.

We must import java.time package to use this class.

In this topic, we will learn how to add months to a date with examples. Syntax of the method is given below.

Syntax

public LocalDate plusMonths (long monthsToAdd)

This method belongs to Java 8 LocalDate class which is stored into java.time package and method description is given below..

Parameters:

monthsToAdd - It represents number of months to add. It may be positive or negative.

Returns:

The plusMonths() method returns a copy of the LocalDate after adding months.

Exception:

This method throws an exception: DateTimeException if the result exceeds the supported date range.

Example: How to Add Months to a Date in java

Lets take an example in which we are adding months to a date by using plusMonths() method. We can see after adding months date is increased.

import java.time.LocalDate;

//Example to Add months to a Date
public class Demo {  
	public static void main(String[] args){  
		
		// Take a date
	    LocalDate date = LocalDate.parse("2020-05-03");
		// Displaying date
		System.out.println("Date : "+date);
		// Add 2 months to the date
		LocalDate newDate = date.plusMonths(2); 
		System.out.println("New Date : "+newDate);
	}
}

Output:

Date : 2020-05-03
New Date : 2020-07-03
 

How to Add a Month to Current Date in Java

Let's take another example in which we are adding a month to the current date. We are using plusMonths() method to add months to a date. Notice, after adding one month to a date, it increased.

The now() method is used to get the current localdate.

import java.time.LocalDate;

//Example to Add months to a Date
public class Demo {  
	public static void main(String[] args){  
		
		// Take a date
	    LocalDate date = LocalDate.now();
		// Displaying date
		System.out.println("Date : "+date);
		// Add 1 month to the date
		LocalDate newDate = date.plusMonths(1); 
		System.out.println("New Date : "+newDate);
	}
}

Output:

Date : 2020-05-03
New Date : 2020-06-03
 

Negative Integer Argument

We can use plusMonths() method to subtract months from a date as well. If we pass negative value as an argument then it will subtract months from the date.

This method works completly opposite if argument is a negative integer. See the following example.

import java.time.LocalDate;

//Example to Subtract months to a Date
public class Demo {  
	public static void main(String[] args){  
		
		// Take a date
	    LocalDate date = LocalDate.parse("2020-05-03");
		// Displaying date
		System.out.println("Date : "+date);
		// Subtract 2 months from a date
		LocalDate newDate = date.plusMonths(-2); // negative integer
		System.out.println("New Date : "+newDate);
	}
}

Output:

Date : 2020-05-03
New Date : 2020-03-03
 

Conclusion

Well, in this topic, we have learned To add number of months to a date which is created by using LocalDate class. This method returns a copy of date after adding the months, we used several examples to explain use of plusMonths() method..

If we missed something, you can suggest us at - info.javaexercise@gmail.com

 

Suggestion:
Must Read: