Period plusMonths() method in Java with Examples
The plusMonths() method of Period class in Java is used to add the specified months to this current period. This functions operates only on MONTHS and does not affect YEARS and DAYS.
Syntax:
public Period plusMonths(long monthsToAdd)
Parameters: This method accepts a single parameter monthsToAdd which is the number of months to be added to the period.
Return Value: This method returns a Period based on provided period in the input adding the specified number of months. It must not be null.
Exceptions: This method throws an ArithmeticException if numeric overflow occurs.
Below program illustrates the above method:
Program 1:
// Java code to show the function plusMonths() // to add the number of months to given periods import java.time.Period; import java.time.temporal.ChronoUnit; public class PeriodClass { // Function to add months to given periods static void addMonths(Period p1, int monthstoAdd) { System.out.println(p1.plusMonths(monthstoAdd)); } // Driver Code public static void main(String[] args) { // Defining first period int year = -4; int months = -11; int days = 0; Period p1 = Period.of(year, months, days); int monthstoAdd = -8; addMonths(p1, monthstoAdd); } } |
P-4Y-19M
Program 2: Months to be added can be negative.
// Java code to show the function plusMonths() // to add the number of months from given periods import java.time.Period; import java.time.temporal.ChronoUnit; public class PeriodClass { // Function to add months to given period static void addMonths(Period p1, int monthstoAdd) { System.out.println(p1.plusMonths(monthstoAdd)); } // Driver Code public static void main(String[] args) { // Defining first period int year = 4; int months = 11; int days = 10; Period p1 = Period.of(year, months, days); int monthstoAdd = 8; addMonths(p1, monthstoAdd); } } |
P4Y19M10D
Reference: https://docs.oracle.com/javase/8/docs/api/java/time/Period.html#plusMonths-long-
Recommended Posts:
- ZonedDateTime plusMonths() method in Java with Examples
- OffsetDateTime plusMonths() method in Java with examples
- LocalDate plusMonths() method in Java with Examples
- LocalDateTime plusMonths() method in Java with Examples
- Period between() method in Java with Examples
- Period get() method in Java with Examples
- Period plus() method in Java with Examples
- Period of() method in Java with Examples
- Period from() method in Java with Examples
- Period ofMonths() method in Java with Examples
- Period getUnits() method in Java with Examples
- Period hashCode() method in Java with Examples
- Period isNegative() method in Java with Examples
- Period minusMonths() method in Java with Examples
- Period minusYears() 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.



