Instant minus() method in Java with Examples

28 Jan, 2022
In Instant class, there are two types of minus() method depending upon the parameters passed to it. 
minus() method of a Instant class used to returns a copy of this instant with the specified amount of unit subtracted.If it is not possible to subtract the amount, because the unit is not supported or for some other reason, an exception is thrown.Syntax:  
public Instant minus(long amountToSubtract,
                     TemporalUnit unit)
Parameters: This method accepts two parameters:  
amountToSubtract which is the amount of the unit to subtract to the result, may be negative and
unit which is the unit of the amount to subtract, not null.
Return value: This method returns Instant based on this instant with the specified amount subtracted.Exception: This method throws following Exceptions:  
DateTimeException – if the subtraction cannot be made
UnsupportedTemporalTypeException – if the unit is not supported
ArithmeticException – if numeric overflow occurs
Below programs illustrate the minus() method:Program 1:  
Java
// Java program to demonstrate// Instant.minus() method import java.time.*;import java.time.temporal.ChronoUnit; public class GFG {    public static void main(String[] args)    {         // create an Instant object        Instant instant            = Instant.parse("2018-12-30T19:34:50.63Z");         // subtract 20 DAYS to Instant        Instant value            = instant.minus(20, ChronoUnit.DAYS);         // print result        System.out.println("Instant after subtracting DAYS: "                           + value);    }}
Instant after subtracting DAYS: 2018-12-10T19:34:50.630Z
 
minus() method of a Instant class used to returns a copy of this instant with the specified amount subtracted to date-time.The amount is typically Period or Duration but may be any other type implementing the TemporalAmount interface.Syntax:  
public Instant minus(TemporalAmount amountTosubtract)
Parameters: This method accepts one single parameter amountTosubtract which is the amount to subtract, It should not be null.Return value: This method returns Instant based on this date-time with the subtraction made, not null.Exception: This method throws following Exceptions:
DateTimeException – if the subtraction cannot be made
ArithmeticException – if numeric overflow occurs
Below programs illustrate the minus() method: Program 1:  
Java
// Java program to demonstrate// Instant.minus() method import java.time.*;public class GFG {    public static void main(String[] args)    {         // create an Instant object        Instant inst            = Instant.parse("2018-12-30T19:34:50.63Z");         // subtract 10 Days to Instant        Instant value            = inst.minus(Period.ofDays(10));         // print result        System.out.println("Instant after subtracting Days: "                           + value);    }}
Instant after subtracting Days: 2018-12-20T19:34:50.630Z
 
References: https://docs.oracle.com/javase/10/docs/api/java/time/Instant.html#minus(java.time.temporal.TemporalAmount) https://docs.oracle.com/javase/10/docs/api/java/time/Instant.html#minus(long, java.time.temporal.TemporalUnit) 
saurabh1990aror
Java-Functions
Java-Instant
Java-time package
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.