duration java
持续时间类from()方法 (Duration Class from() method)
from() method is available in java.time package.
from()方法在java.time包中可用。
from() method is used to return a copy of this Duration from the given TemporalAmount.
from()方法用于从给定的TemporalAmount返回此Duration的副本。
from() 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.
from()方法是一个静态方法,可以使用类名进行访问,如果尝试使用类对象访问该方法,则不会收到错误。
from() method may throw an exception at the time of returning Duration from the given amount.
从给定数量返回Duration时, from()方法可能会引发异常。
- DateTimeException: This exception may throw when the given amt couldn't convert into a Duration.DateTimeException :当给定的amt无法转换为Duration时,可能引发此异常。
- ArithmeticException: This exception may throw when the calculated exceeds the limit.ArithmeticException :当计算的值超出限制时,可能引发此异常。
Syntax:
句法:
public static Duration from(TemporalAmount amt);
Parameter(s):
参数:
TemporalAmount amt – represents the amount to convert to a Duration.
TemporalAmount amt –表示要转换为Duration的金额。
Return value:
返回值:
The return type of this method is Duration, it returns the Duration that is calculated from the given amount.
此方法的返回类型为Duration ,它返回从给定数量计算得出的Duration。
Example:
例:
// Java program to demonstrate the example
// of Duration from(TemporalAmount amt) method of Duration
import java.time.*;
public class FromOfDuration {
public static void main(String args[]) {
// Instantiates a Duration object
Duration du1 = Duration.ofHours(10);
// Display du1
System.out.println("du1: " + du1);
// returns an instance of
// Duration from the given temporal
// amount i.e. here the given amount is
// 10 hrs so it generates a new Duration
// (du_from) for the given amount
Duration du_from = Duration.from(du1);
// Display du_from
System.out.println("du_from: " + du_from);
// Here, we are converting 10 hrs into Minutes
System.out.println("du_from.toMinutes(): " + du_from.toMinutes());
// Here, we are converting 10 hrs into Seconds
System.out.println("du_from.toSeconds(): " + du_from.toSeconds());
}
}
Output
输出量
du1: PT10H
du_from: PT10H
du_from.toMinutes(): 600
du_from.toSeconds(): 36000
翻译自: https://www.includehelp.com/java/duration-from-method-with-example.aspx
duration java