ChronoPeriod minus() method in Java with Examples
Last Updated :
27 Jun, 2019
The minus() method of ChronoPeriod class in Java is used to subtract the given amount of period from the specified period. This function operates separately on YEAR, MONTH and DAY.
Note: Normalization is not performed. 12 months and 1 year are different.
Syntax:
ChronoPeriod minus(TemporalAmount amountToSubtract)
Parameters: This method accepts a single parameter amountToSubtract, which is the amount to subtract from this period. It must not be null.
Return Value: This method returns a ChronoPeriod based on the given period with the requested period subtracted, and this must not be null.
Exceptions:
- DateTimeException: This exception is returned if the specified amount has a non-ISO chronology or contains an invalid unit.
- ArithmeticException: This exception is caught if numeric overflow occurs.
Below programs illustrate the above method:
Program 1:
import java.time.*;
import java.time.chrono.*;
import java.time.temporal.ChronoUnit;
public class ChronoPeriodDemo {
static void subtractChronoPeriod(ChronoPeriod p1, ChronoPeriod p2)
{
System.out.println(p1.minus(p2));
}
public static void main(String[] args)
{
int year = 4 ;
int months = 11 ;
int days = 10 ;
ChronoPeriod p1 = Period.of(year, months, days);
int year1 = 2 ;
int months1 = 7 ;
int days1 = 8 ;
ChronoPeriod p2 = Period.of(year1, months1, days1);
subtractChronoPeriod(p1, p2);
}
}
|
Program 2:
import java.time.*;
import java.time.chrono.*;
import java.time.temporal.ChronoUnit;
public class ChronoPeriodDemo {
static void subtractChronoPeriod(ChronoPeriod p1, ChronoPeriod p2)
{
System.out.println(p1.minus(p2));
}
public static void main(String[] args)
{
int year1 = 2 ;
int months1 = 7 ;
int days1 = 8 ;
ChronoPeriod p1 = Period.of(year1, months1, days1);
int year = 4 ;
int months = 11 ;
int days = 10 ;
ChronoPeriod p2 = Period.of(year, months, days);
subtractChronoPeriod(p1, p2);
}
}
|
Reference: https://docs.oracle.com/javase/9/docs/api/java/time/chrono/ChronoPeriod.html#minus-java.time.temporal.TemporalAmount-
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...