Thursday, October 23, 2025
HomeLanguagesJavaYear plusYears() Method in Java with Examples

Year plusYears() Method in Java with Examples

plusYears() method of the Year class used to a copy of this Year after adding the specified number of years to this year.
This instance is immutable and unaffected by this method call.

Syntax:

public Year plusYears(long yearsToadd)

Parameters: This method accepts yearsToadd as parameter which is the years to add, may be negative.

Return value: This method returns Year based on this year with the years added.

Exception: This method throws following Exceptions:

  • DateTimeException – if the result exceeds the supported range.

Below programs illustrate the plusYears() method:
Program 1:




// Java program to demonstrate
// Year.plusYears() method
  
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // create a Year object
        Year year = Year.of(2019);
  
        // print instance
        System.out.println("Year :"
                           + year);
  
        // apply plusYears method
        Year value
            = year.plusYears(20);
  
        // print result
        System.out.println("After addition year: "
                           + value);
    }
}


Output:

Year :2019
After addition year: 2039

Program 2:




// Java program to demonstrate
// Year.plusYears() method
  
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // create a Year object
        Year year = Year.of(2019);
  
        // print instance
        System.out.println("Year :"
                           + year);
  
        // apply plusYears method
        Year value
            = year.plusYears(1200);
  
        // print result
        System.out.println("After addition year: "
                           + value);
    }
}


Output:

Year :2019
After addition year: 3219

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

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS