Friday, April 3, 2026
HomeLanguagesJavaYear minusYears() method in Java with Examples

Year minusYears() method in Java with Examples

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

Syntax:

public Year minusYears(long yearsToSubtract)

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

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

Exception: This method throws following Exceptions:

  • DateTimeException – if the result exceeds the supported range.

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




// Java program to demonstrate
// Year.minusYears() 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 minusYears method
        Year value
            = year.minusYears(20);
  
        // print result
        System.out.println("After subtraction year: "
                           + value);
    }
}


Output:

Year :2019
After subtraction year: 1999

Program 2:




// Java program to demonstrate
// Year.minusYears() 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 minusYears method
        Year value
            = year.minusYears(1200);
  
        // print result
        System.out.println("After subtraction year: "
                           + value);
    }
}


Output:

Year :2019
After subtraction year: 819

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

RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32512 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6885 POSTS0 COMMENTS
Nicole Veronica
12006 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12100 POSTS0 COMMENTS
Shaida Kate Naidoo
7015 POSTS0 COMMENTS
Ted Musemwa
7259 POSTS0 COMMENTS
Thapelo Manthata
6971 POSTS0 COMMENTS
Umr Jansen
6960 POSTS0 COMMENTS