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