duration java_Java Duration类| of()方法与示例

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值