duration java
()方法的持续时间类 (Duration Class of() method)
of() method is available in java.time package.
of()方法在java.time包中可用。
of() method is used to denotes the given amount (amt) in the given unit (t_unit) in this Duration.
of()方法用于表示此Duration中给定单位(t_unit)中的给定金额(amt)。
of() 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.
of()方法是一个静态方法,可以使用类名进行访问,如果尝试使用类对象访问该方法,则不会出错。
of() method may throw an exception at the time of representing Duration.
of()方法在表示Duration时可能会引发异常。
- ArithmeticException: This exception may throw when the calculated result value exceeds the limit.ArithmeticException :当计算结果值超过限制时,可能引发此异常。
- DateTimeException: This exception may throw when the given unit is not exact in terms of Duration.DateTimeException :如果给定单位的持续时间不正确,则可能引发此异常。
Syntax:
句法:
public static Duration of(long amt, TemporalUnit t_unit);
Parameter(s):
参数:
long amt – represents the amount in which to use in the given unit.
long amt –表示以给定单位使用的数量。
TemporalUnit t_unit – represents the unit by which to measure the amount.
TemporalUnit t_unit –表示测量数量的单位。
Return value:
返回值:
The return type of this method is Duration, it returns the Duration that holds the amount (amt) value in the given unit.
此方法的返回类型为Duration ,它返回以给定单位保存金额(amt)值的Duration。
Example:
例:
// Java program to demonstrate the example
// of of(long amt, TemporalUnit t_unit) method
// of Duration
import java.time.*;
import java.time.temporal.*;
public class OfDuration {
public static void main(String args[]) {
long amt = 2;
ChronoUnit amt_unit = ChronoUnit.MINUTES;
// represents the given amount
// in the given unit i.e here amt 2 is
// represented in minutes like 2M
Duration du1 = Duration.of(amt, amt_unit);
// Display du1
System.out.println("Duration.of(2,minutes): " + du1);
amt_unit = ChronoUnit.DAYS;
// represents the given amount
// in the given unit i.e. here amt 2 is
// represented in DAYS like 2D i.e. 48H
Duration du2 = Duration.of(amt, amt_unit);
// Display du2
System.out.println("Duration.of(2,days): " + du2);
}
}
Output
输出量
Duration.of(2,minutes): PT2M
Duration.of(2,days): PT48H
翻译自: https://www.includehelp.com/java/duration-of-method-with-example.aspx
duration java