duration java
持续时间类between()方法 (Duration Class between() method)
between() method is available in java.time package.
between()方法在java.time包中可用。
between() method is used to get the duration between the two given objects and here the first object value is inclusive whereas the second object value is exclusive.
between()方法用于获取两个给定对象之间的持续时间,此处第一个对象值是包含值,而第二个对象值是排除值。
between() method is a static method, it is accessible with the class name and if we try to access the method with the class object then we will not get an error.
between()方法是一个静态方法,可以使用类名进行访问,如果尝试使用类对象访问该方法,则不会收到错误。
between() method may throw an exception at the time of representing duration between objects.
between()方法可能在表示对象之间的持续时间时引发异常。
- DateTimeException: This exception may throw when the seconds lie in between the given object that couldn't be generated.DateTimeException :如果无法生成给定对象之间的时间间隔为秒,则可能引发此异常。
- ArithmeticException: This exception may throw when the calculated result limit exceeds.ArithmeticException :当计算的结果限制超过时,可能会引发此异常。
Syntax:
句法:
public static Duration between(Temporal st_time, Temporal en_time);
Parameter(s):
参数:
Temporal st_time – represents the starting endpoint of the returned duration.
时间st_time –代表返回的持续时间的起始端点。
Temporal en_time – represents the ending endpoint of the returned duration.
时间en_time –表示返回持续时间的结束端点。
Return value:
返回值:
The return type of this method is Duration, it returns Duration that holds the value between the given two objects.
此方法的返回类型为Duration ,它返回Duration ,其中Duration保留给定两个对象之间的值。
Example:
例:
// Java program to demonstrate the example
// of between(Temporal st_time, Temporal en_time)
// method of Duration
import java.time.*;
import java.time.temporal.*;
public class BetweenOfDuration {
public static void main(String args[]) {
// Instantiates a Duration and LocalTime object
Duration du = Duration.ofHours(2);
LocalTime l_time1 = LocalTime.now();
// Display l_time1
System.out.println("l_time1: " + l_time1);
// adds this object (du) into the
// given object (l_time1) i.e. we are adding
// duration (2 hrs) in l_time1 hours unit
LocalTime l_time2 = (LocalTime) du.addTo(l_time1);
// Display l_time2
System.out.println("l_time2: " + l_time2);
// returns the duration between
// Temporal1 and Temporal2 i.e. difference of
// 2 hrs in between l_time1 and l_time2
Duration diff_in_hrs = Duration.between(l_time1, l_time2);
System.out.println("Duration.between(l_time1,l_time2): " + diff_in_hrs);
}
}
Output
输出量
l_time1: 17:26:11.963034
l_time2: 19:26:11.963034
Duration.between(l_time1,l_time2): PT2H
翻译自: https://www.includehelp.com/java/duration-between-method-with-example.aspx
duration java