minus
即时类minus()方法 (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()方法在java.time包中可用。
minus(TemporalAmount t_amt) method is used to subtract the given amount from this Instant and return the Instant.
minus(TemporalAmount t_amt)方法用于从此Instant减去给定数量并返回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.
minus(long amt,TemporalUnit t_unit)方法用于从此Instant减去给定单位的给定数量,并返回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.ArithmeticException :当计算结果超出表示此对象的限制时,可能引发此异常。
- DateTimeException: This exception may throw when getting any error during subtraction.DateTimeException :在减法过程中遇到任何错误时,都可能引发此异常。
- UnsupportedTemporalTypeException: This exception may throw when the given unit is unsupported.UnsupportedTemporalTypeException :当不支持给定单元时,可能引发此异常。
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)",
在第一种情况下,“ minus(TemporalAmount t_amt)”,
- TemporalAmount t_amt – represents the amount to be subtracted from this Instant.
- TemporalAmount t_amt –表示要从此Instant减去的数量。
In the second case, "minus(long amt, TemporalUnit t_unit)",
在第二种情况下,“减号(long amt,TemporalUnit t_unit)”,
- long amt – represents the amount in units to be subtracted from this Instant.
- long amt-表示要从此Instant减去的金额。
- TemporalUnit t_unit – represents the unit to measure the given amount.
- TemporalUnit t_unit –代表测量给定数量的单位。
Return value:
返回值:
In both the cases, the return type of the method is Instant,
在这两种情况下,方法的返回类型均为Instant 。
In the first case, it returns the Instant that holds the value subtracted the given amount from this Instant.
在第一种情况下,它返回的Instant保持从该Instant减去给定值的值。
In the second case, it returns the Instant that holds the value subtracted the given amount in unit from this Instant.
在第二种情况下,它返回的Instant保持从该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
翻译自: https://www.includehelp.com/java/instant-minus-method-with-example.aspx
minus