duration java
持续时间类减去方法 (Duration Class subtractFrom() method)
subtractFrom() method is available in java.time package.
excludeFrom ()方法在java.time包中可用。
subtractFrom() method is used to return the Temporal object subtracted this Duration from the given Temporal object.
excludeFrom()方法用于返回从给定的Temporal对象减去此Duration的Temporal对象。
subtractFrom() 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.
tractorFrom()方法是一个非静态方法,只能使用类对象访问,如果尝试使用类名访问该方法,则会收到错误消息。
subtractFrom() method may throw an exception at the time of performing subtraction.
在执行减法时, subtractFrom()方法可能会引发异常。
- ArithmeticException: This exception may throw when the calculated results exceed the length of this Duration.ArithmeticException :当计算结果超过此Duration的长度时,可能引发此异常。
- DateTimeException: This exception may throw when the given object couldn't subtract this Duration.DateTimeException :如果给定对象无法减去此Duration,则可能引发此异常。
Syntax:
句法:
public Temporal subtractFrom(Temporal temp);
Parameter(s):
参数:
Temporal temp – represents the object by which to subtract this Duration.
临时温度 –表示要减去此工期的对象。
Return value:
返回值:
The return type of this method is Duration, it returns the Temporal that holds the value subtracted this Duration from the given Temporal object.
此方法的返回类型为Duration ,它返回Temporal,该Temporal保留从给定Temporal对象减去此Duration的值。
Example:
例:
// Java program to demonstrate the example
// of subtractFrom(Temporal temp) method
// of Duration
import java.time.*;
import java.time.temporal.*;
public class SubtractFromDuration {
public static void main(String args[]) {
// Instantiates a Duration
// and LocalTime object
Duration du = Duration.ofHours(2);
LocalTime l_time = LocalTime.now();
// Display du and l_time
System.out.println("du to subtract: " + du);
System.out.println("l_time: " + l_time);
// subtracts this object (du) from the given
// object (l_time) i.e. we are subtracting
// duration (2 hrs) from l_time hours unit
l_time = (LocalTime) du.subtractFrom(l_time);
// Display updated l_time
System.out.println("du.subtractFrom(l_time): " + l_time);
}
}
Output
输出量
du to subtract: PT2H
l_time: 08:20:02.395444
du.subtractFrom(l_time): 06:20:02.395444
翻译自: https://www.includehelp.com/java/duration-subtractfrom-method-with-example.aspx
duration java