Home »
Java »
Java Reference »
Java Instant Class
Java Instant Class | minus() Method with Example
Instant Class minus() method: Here, we are going to learn about the minus() method of Instant Class with its syntax and example.
Submitted by Preeti Jain, on May 28, 2020
Instant Class minus() method
Syntax:
public Instant minus(TemporalAmount t_amt);
public Instant minus(long amt, TemporalUnit t_unit);
- minus() method is available in java.time package.
- minus(TemporalAmount t_amt) method is used to subtract the given amount from this Instant and return the Instant.
- minus(long amt, TemporalUnit t_unit) method is used to subtract the given amount in the given unit from this Instant and return the Instant.
-
These methods may throw an exception at the time of performing subtraction.
- ArithmeticException: This exception may throw when the calculated result exceeds the limit to represent this object.
- DateTimeException: This exception may throw when getting any error during subtraction.
- UnsupportedTemporalTypeException: This exception may throw when the given unit is unsupported.
- These are non-static methods and it is accessible with class objects and if we try to access these methods with class name then we will get an error.
Parameter(s):
-
In the first case, "minus(TemporalAmount t_amt)",
- TemporalAmount t_amt – represents the amount to be subtracted from this Instant.
-
In the second case, "minus(long amt, TemporalUnit t_unit)",
- long amt – represents the amount in units to be subtracted from this Instant.
- TemporalUnit t_unit – represents the unit to measure the given amount.
Return value:
In both the cases, the return type of the method is Instant,
- In the first case, it returns the Instant that holds the value subtracted the given amount from this Instant.
- In the second case, it returns the Instant that holds the value subtracted the given amount in unit from this Instant.
Example:
// Java program to demonstrate the example
// of minus() method of Instant
import java.time.*;
import java.time.temporal.*;
public class MinusOfInstant {
public static void main(String args[]) {
long amt = 2;
// Instantiates two Instant and
// a Duration object
Instant ins1 = Instant.parse("2006-04-03T05:10:15.00Z");
Instant ins2 = Instant.now();
Duration du = Duration.ofSeconds(2);
// Display ins1,ins2
System.out.println("Instant ins1 and ins2: ");
System.out.println("ins1: " + ins1);
System.out.println("ins2: " + ins2);
System.out.println("amt to subtract: " + amt);
System.out.println("du to subtract: " + du);
System.out.println();
// Here, this method subtracts the given amount
// in the given unit from this Instant i.e. here
// we are subtracting 2 hours from Instant ins1
Instant minus_val = ins1.minus(amt, ChronoUnit.HOURS);
// Display minus_val
System.out.println("ins1.minus(amt,ChronoUnit.HOURS): " + minus_val);
// Here, this method subtracts the given amount
// from this Instant i.e. here we are subtracting
// 2 seconds from Instant ins2
minus_val = ins2.minus(du);
// Display minus_val
System.out.println("ins2.minus(du): " + minus_val);
}
}
Output
Instant ins1 and ins2:
ins1: 2006-04-03T05:10:15Z
ins2: 2020-05-28T01:06:04.706169Z
amt to subtract: 2
du to subtract: PT2S
ins1.minus(amt,ChronoUnit.HOURS): 2006-04-03T03:10:15Z
ins2.minus(du): 2020-05-28T01:06:02.706169Z