Period plusYears() method in Java with Examples
The plusYears() method of Period class in Java is used to add the given years to this period. This functions operates only on YEARS and does not affect MONTH and DAYS.
Syntax:
public Period plusYears(long yearsToAdd)
Parameters: This method accepts a single parameter yearsToAdd which is the number of years 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 years. It must not be null.
Exceptions: It throws an ArithmeticException. This exception is caught if numeric overflow occurs.
Below programs illustrate the above method:
Program 1:
// Java code to show the function plusYears() // to add the number of years from given periods import java.time.Period; import java.time.temporal.ChronoUnit; public class PeriodClass { // Function to add years to given period static void addYears(Period p1, int yearstoAdd) { System.out.println(p1.plusYears(yearstoAdd)); } // 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 yearstoAdd = 8; addYears(p1, yearstoAdd); } } |
P12Y11M10D
Program 2: Period can be negative.
// Java code to show the function plusYears() // to add the number of years to given periods import java.time.Period; import java.time.temporal.ChronoUnit; public class PeriodClass { // Function to add years to given periods static void addYears(Period p1, int yearstoAdd) { System.out.println(p1.plusYears(yearstoAdd)); } // 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 yearstoAdd = 8; addYears(p1, yearstoAdd); } } |
P4Y-11M
Reference: https://docs.oracle.com/javase/8/docs/api/java/time/Period.html#plusYears-long-
Recommended Posts:
- LocalDateTime plusYears() method in Java with Examples
- Year plusYears() Method in Java with Examples
- LocalDate plusYears() method in Java with Examples
- ZonedDateTime plusYears() method in Java with Examples
- OffsetDateTime plusYears() method in Java with examples
- Period from() method in Java with Examples
- Period get() method in Java with Examples
- Period between() method in Java with Examples
- Period of() method in Java with Examples
- Period plus() method in Java with Examples
- Period ofDays() method in Java with Examples
- Period toTotalMonths() method in Java with Examples
- Period withDays() method in Java with Examples
- Period withYears() 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.



