Period minusYears() method in Java with Examples
Last Updated :
28 Nov, 2018
The minusYears() method of Period class in Java is used to subtract the specified years from given period. This functions operates only on YEARS and does not affect other two MONTHS and DAYS.
Syntax:
public Period minusYears(long yearsToSubtract)
Parameters: This method accepts a single parameter yearsToSubtract which is the number of year to be subtracted from the period.
Returns Value: This method returns a Period based on provided period in the input subtracting the specified number of years. It must not be null.
Exceptions: It throws an ArithmeticException. This exception is caught if numeric overflow occurs.
Below programs illustrate the above method:
Program 1:
import java.time.Period;
import java.time.temporal.ChronoUnit;
public class PeriodClass {
static void subtractYears(Period p1, int yearstoSubtract)
{
System.out.println(p1.minusYears(yearstoSubtract));
}
public static void main(String[] args)
{
int year = 4 ;
int months = 11 ;
int days = 10 ;
Period p1 = Period.of(year, months, days);
int yearstoSubtract = 8 ;
subtractYears(p1, yearstoSubtract);
}
}
|
Program 2:
import java.time.Period;
import java.time.temporal.ChronoUnit;
public class PeriodClass {
static void subtractYears(Period p1, int yearstoSubtract)
{
System.out.println(p1.minusYears(yearstoSubtract));
}
public static void main(String[] args)
{
int year = 4 ;
int months = 11 ;
int days = 10 ;
Period p1 = Period.of(year, months, days);
int yearstoSubtract = - 8 ;
subtractYears(p1, yearstoSubtract);
}
}
|
Reference: https://docs.oracle.com/javase/8/docs/api/java/time/Period.html#minusYears-long-
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...