Instant minus() method in Java with Examples
In Instant class, there are two types of minus() method depending upon the parameters passed to it.
minus(long amountTosubtract, TemporalUnit unit)
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 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(TemporalAmount amountTosubtract)
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 subtractition 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 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)
Recommended Posts:
- ChronoPeriod minus() method in Java with Examples
- DayOfWeek minus() method in Java with Examples
- LocalTime minus() method in Java with Examples
- LocalDateTime minus() method in Java with Examples
- Period minus() method in Java with Examples
- LocalDate minus() method in Java with Examples
- OffsetTime minus() method in Java with examples
- ZonedDateTime minus() method in Java with Examples
- Instant from() method in Java with Examples
- Instant until() Method in Java with Examples
- Instant now() Method in Java with Examples
- Instant plus() method in Java with Examples
- Instant get() method in Java with Examples
- Instant with() Method in Java with Examples
- ChronoLocalDateTime minus(TemporalAmount) method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



