java.time.Year.minus() Method Example



Description

The java.time.Year.minus(long amountToSubtract, TemporalUnit unit) method returns a copy of this year with the specified amount subtracted.

Declaration

Following is the declaration for java.time.Year.minus(long amountToSubtract, TemporalUnit unit) method.

public Year minus(long amountToSubtract, TemporalUnit unit)

Parameters

  • amountToSubtract − the amount of the unit to subtract from the result, may be negative.

  • unit − the unit of the amount to subtract, not null.

Return Value

a Year based on this date with the specified amount 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(long amountToSubtract, TemporalUnit unit) method.

package com.tutorialspoint;

import java.time.Year;
import java.time.temporal.ChronoUnit;

public class YearDemo {
   public static void main(String[] args) {
 
      Year date = Year.of(2017);
      System.out.println(date.minus(2,ChronoUnit.YEARS));  
   }
}

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

2015
Advertisements