Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

LocalDate plusYears() method in Java with Examples

  • Last Updated : 30 Jul, 2019

The plusYears() method of LocalDate class in Java is used to add the number of specified years in this LocalDate and return a copy of LocalDate.

This method adds the years field in the following steps:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

  • Add the years to the year field.
  • Check if the date after adding years is valid or not.
  • If date is invalid then method adjust the day-of-month to the last valid day.

For example, 2016-02-29 (leap year) plus one year gives date 2017-02-29 but this is invalid result, so the last valid day of the month, 2017-02-28, is returned.This instance is immutable and unaffected by this method call.

Syntax:



public LocalDate plusYears(long yearsToAdd)

Parameters: This method accepts a single parameter yearsToAdd which represents the years to add, may be negative.

Return value: This method returns a LocalDate based on this date with the years added, not null.

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

Below programs illustrate the plusYears() method:

Program 1:




// Java program to demonstrate
// LocalDate.plusYears() 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 years: " + date);
  
        // add 3 years
        LocalDate returnvalue
            = date.plusYears(3);
  
        // print result
        System.out.println("LocalDate after "
                           + " adding years: " + returnvalue);
    }
}
Output:
LocalDate before adding years: 2018-11-13
LocalDate after  adding years: 2021-11-13

Program 2:




// Java program to demonstrate
// LocalDate.plusYears() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a LocalDate object
        LocalDate date
            = LocalDate.parse("2016-02-29");
  
        // print instance
        System.out.println("LocalDate before"
                           + " adding years: " + date);
  
        // add 2 years
        LocalDate returnvalue
            = date.plusYears(2);
  
        // print result
        System.out.println("LocalDate after "
                           + " adding years: " + returnvalue);
    }
}
Output:
LocalDate before adding years: 2016-02-29
LocalDate after  adding years: 2018-02-28

References:
https://docs.oracle.com/javase/10/docs/api/java/time/LocalDate.html#plusYears(long)




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!