ChronoPeriod subtractFrom() method in Java with Examples
The subtractFrom(Temporal) method of ChronoPeriod Interface in java.time.chrono package is used subtract this ChronoPeriod to the specified temporal object, passed as the parameter.
Syntax:
Temporal subtractFrom(Temporal temporalObject)
Parameters: This method accepts a parameter temporalObject which is the amount to be adjusted in this ChronoPeriod. It should not be null.
Return Value: This method returns an object of the same type with the temporalObject adjusted to it.
Exception: This method throws:
- DateTimeException: if unable to subtract.
- ArithmeticException: if numeric overflow occurs.
Below examples illustrate the ChronoPeriod.subtractFrom() method:
Program 1:
import java.time.*;
import java.time.chrono.*;
import java.time.temporal.ChronoUnit;
public class ChronoPeriodDemo {
public static void main(String[] args)
{
int year = 4 ;
int months = 11 ;
int days = 10 ;
ChronoPeriod p1 = Period.of(year, months, days);
LocalDateTime currentTime
= LocalDateTime.now();
System.out.println(
p1.subtractFrom(currentTime));
}
}
|
Output:
2014-07-07T14:22:21.929
Program 2:
import java.time.*;
import java.time.chrono.*;
import java.time.temporal.ChronoUnit;
public class ChronoPeriodDemo {
public static void main(String[] args)
{
int year1 = 2 ;
int months1 = 7 ;
int days1 = 8 ;
ChronoPeriod p1 = Period.of(year1, months1, days1);
LocalDateTime currentTime
= LocalDateTime.now();
System.out.println(
p1.subtractFrom(currentTime));
}
}
|
Output:
2016-11-09T14:22:26.600
Reference: https://docs.oracle.com/javase/9/docs/api/java/time/chrono/ChronoPeriod.html#subtractFrom-java.time.temporal.Temporal-
Last Updated :
24 Jun, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...