duration java
持续时间类minusSeconds()方法 (Duration Class minusSeconds() method)
minusSeconds() method is available in java.time package.
minusSeconds()方法在java.time包中可用。
minusSeconds() method is used to subtract the given duration in seconds from this Duration and returns the Duration.
minusSeconds()方法用于从此Duration中减去给定的持续时间(以秒为单位),并返回Duration。
minusSeconds() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
minusSeconds()方法是一个非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
minusSeconds() method may throw an exception at the time of performing subtraction.
minusSeconds()方法在执行减法时可能会引发异常。
ArithmeticException: This exception may throw when the calculated result value exceeds the limit.
ArithmeticException :当计算结果值超过限制时,可能引发此异常。
Syntax:
句法:
public Duration minusSeconds(long sec_val);
Parameter(s):
参数:
long sec_val – represents the seconds to be subtracted from this Duration.
long sec_val –表示要从该持续时间中减去的秒数。
Return value:
返回值:
The return type of this method is Duration, it returns the Duration that holds the value subtracted the given duration in seconds from this Duration.
此方法的返回类型为Duration ,它返回的Duration包含从该Duration中减去以秒为单位的给定持续时间的值。
Example:
例:
// Java program to demonstrate the example
// of minusSeconds(long sec_val) method of Duration
import java.time.*;
public class MinusSecondsOfDuration {
public static void main(String args[]) {
long seconds = 10;
// Instantiates two Duration objects
Duration du1 = Duration.ofSeconds(40);
Duration du2 = Duration.ofMinutes(1);
// Display du1, du2
System.out.println("du1: " + du1);
System.out.println("du2: " + du2);
System.out.println("seconds to subtract: " + seconds);
System.out.println();
// subtracts the given duration
// in seconds from this Duration du1
// and returns the Duration i.e. here we
// are substracting the given 10 seconds
// from this du1 that holds the value of
// 40 seconds
Duration minus_val = du1.minusSeconds(seconds);
// Display minus_val
System.out.println("du1.minusSeconds(seconds): " + minus_val);
// subtracts the given duration
// in seconds from this Duration du2
// and returns the Duration i.e. here we
// are substracting the given 10 seconds
// from this du2 that holds the value of
// 1 minutes = 60S i.e. (60S - 10S)
minus_val = du2.minusSeconds(seconds);
// Display minus_val
System.out.println("du2.minusSeconds(seconds): " + minus_val);
}
}
Output
输出量
du1: PT40S
du2: PT1M
seconds to subtract: 10
du1.minusSeconds(seconds): PT30S
du2.minusSeconds(seconds): PT50S
翻译自: https://www.includehelp.com/java/duration-minusseconds-method-with-example.aspx
duration java