LocalDate plusMonths() method in Java with Examples
The plusMonths() method of LocalDate class in Java is used to add the number of specified months in this LocalDate and return a copy of LocalDate.
This method adds the months field in the following steps:
- Add the months to the month-of-year field.
- Check if the date after adding months is valid or not.
- If date is invalid then method adjust the day-of-month to the last valid day.
For example, 2018-08-31 plus one month gives date 2018-09-31 but this is invalid result, so the last valid day of the month, 2018-09-30, is returned.This instance is immutable and unaffected by this method call.
Syntax:
public LocalDate plusMonths(long monthsToAdd)
Parameters: This method accepts a single parameter monthsToAdd which represents the months to add, may be negative.
Return Value: This method returns a LocalDate based on this date with the months added, not null.
Exception: This method throws DateTimeException if the result exceeds the supported date range.
Below programs illustrate the plusMonths() method:
Program 1:
// Java program to demonstrate // LocalDate.plusMonths() method import java.time.*; public class GFG { public static void main(String[] args) { // create a LocalDate object LocalDate date = LocalDate.parse("2018-11-13"); // print instance System.out.println("LocalDate before" + " adding months: " + date); // add 5 months LocalDate returnvalue = date.plusMonths(5); // print result System.out.println("LocalDate after " + " adding months: " + returnvalue); } } |
LocalDate before adding months: 2018-11-13 LocalDate after adding months: 2019-04-13
Program 2:
// Java program to demonstrate // LocalDate.plusMonths() method import java.time.*; public class GFG { public static void main(String[] args) { // create a LocalDate object LocalDate date = LocalDate.parse("2018-12-31"); // print instance System.out.println("LocalDate before" + " adding months: " + date); // add 9 months LocalDate returnvalue = date.plusMonths(9); // print result System.out.println("LocalDate after " + " adding months: " + returnvalue); } } |
LocalDate before adding months: 2018-12-31 LocalDate after adding months: 2019-09-30
References:
https://docs.oracle.com/javase/10/docs/api/java/time/LocalDate.html#plusMonths(long)
Recommended Posts:
- ZonedDateTime plusMonths() method in Java with Examples
- LocalDateTime plusMonths() method in Java with Examples
- OffsetDateTime plusMonths() method in Java with examples
- Period plusMonths() method in Java with Examples
- LocalDate plus() method in Java with Examples
- LocalDate with() Method in Java with Examples
- LocalDate get() method in Java with Examples
- LocalDate now() Method in Java with Examples
- LocalDate now() Method in Java with Examples
- LocalDate isEqual() method in Java with Examples
- LocalDate lengthOfMonth() method in Java with Examples
- LocalDate isLeapYear() method in Java with Examples
- LocalDate lengthOfYear() method in Java with Examples
- LocalDate query() Method in Java with Examples
- LocalDate minusMonths() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai



