minus
持续时间类minus()方法 (Duration Class minus() method)
Syntax:
句法:
public Duration minus(Duration d);
public Duration minus(long amt, TemporalUnit t_unit);
minus() method is available in java.time package.
minus()方法在java.time包中可用。
minus(Duration d) method is used to subtract the given Duration from this Duration and returns the Duration.
minus(Duration d)方法用于从此Duration中减去给定的Duration并返回Duration。
minus(long amt, TemporalUnit t_unit) method is used to subtract the given amount in the given units from this Duration and return the Duration.
minus(long amt,TemporalUnit t_unit)方法用于从此Duration中减去给定单位的给定数量,并返回Duration。
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 :当计算结果超出表示此对象的限制时,可能引发此异常。
These are non-static methods and it is accessible with class objects and if we try to access these methods with the class name then we will get an error.
这些是非静态方法,可通过类对象访问,如果尝试使用类名访问这些方法,则会收到错误消息。
Parameter(s):
参数:
In the first case, minus(Duration d),
在第一种情况下, minus(Duration d) ,
- Duration d – represents the Duration to be subtracted from this Duration.
- 持续时间d –表示要从该持续时间中减去的持续时间。
In the first case, minus(long amt, TemporalUnit t_unit),
在第一种情况下, minus(long amt,TemporalUnit t_unit) ,
- long amt – represents the amount in units to be subtracted from this Duration.
- long amt-表示要从此工期中减去的金额。
- 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 Duration,
在这两种情况下,方法的返回类型均为Duration 。
In the first case, it returns the Duration that holds the value subtracted the given duration from this Duration.
在第一种情况下,它返回持续时间,该持续时间包含从该持续时间中减去给定持续时间的值。
In the second case, it returns the Duration that holds the value subtracted the given amount in a unit from this Duration.
在第二种情况下,它返回持续时间,该持续时间保存的值是从该持续时间中减去一个单位的给定值。
Example:
例:
// Java program to demonstrate the example
// of minus() method of Duration
import java.time.*;
import java.time.temporal.*;
public class MinusOfDuration {
public static void main(String args[]) {
long amt = 10;
// Instantiates two Duration objects
Duration du1 = Duration.ofMinutes(3);
Duration du2 = Duration.parse("P2DT2H20M20S");
// Display du1, du2
System.out.println("du1: " + du1);
System.out.println("du2: " + du2);
System.out.println("amt to subtract: " + amt);
System.out.println();
// subtracts the given duration
// from this Duration du2 and returns
// the Duration i.e. here we subtract
// the given 3 minutes from this du2 that holds
// the value of 2D:2H:20M:20S
Duration minus_val = du2.minus(du1);
// Display minus_val
System.out.println("du2.minus(du1): " + minus_val);
// subtracts the given amount
// in the given unit from this Duration
// and returns the Duration i.e.
// here we are subtracting the amt
// 10 in minutes unit from this du2
// that holds the value of 2D:2H:20M:20S
minus_val = du2.minus(amt, ChronoUnit.MINUTES);
// Display minus_val
System.out.println("du2.minus(amt,ChronoUnit.MINUTES): " + minus_val);
}
}
Output
输出量
du1: PT3M
du2: PT50H20M20S
amt to subtract: 10du2.minus(du1): PT50H17M20S
du2.minus(amt,ChronoUnit.MINUTES): PT50H10M20S
翻译自: https://www.includehelp.com/java/duration-minus-method-with-example.aspx
minus