java.time.Year.minus() Method Example



Description

The java.time.Year.minus(TemporalAmount amountToSubtract) method returns a copy of this Year with the specified Year subtracted.

Declaration

Following is the declaration for java.time.Year.minus(TemporalAmount amountToSubtract) method.

public Year minus(TemporalAmount amountToSubtract)

Parameters

amountToSubtract − the amount to subtract, not null.

Return Value

a Year based on this Year with the specified Year subtracted, not null.

Exception

  • DateTimeException − if the subtraction cannot be made.

  • UnsupportedTemporalTypeException − if the unit is not supported.

  • ArithmeticException − if numeric overflow occurs.

Example

The following example shows the usage of java.time.Year.minus(TemporalAmount amountToSubtract) method.

package com.tutorialspoint;

import java.time.Period;
import java.time.Year;
import java.time.temporal.ChronoField;

public class YearDemo {
   public static void main(String[] args) {

      Year date = Year.of(2017); 
      System.out.println(date.get(ChronoField.YEAR));
      System.out.println(date.minus(Period.ofYears(4)).get(ChronoField.YEAR));
   }
}

Let us compile and run the above program, this will produce the following result −

2017
2013
Advertisements