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

duration java

Duration类的Second()方法 (Duration Class ofSeconds() method)

Syntax:

句法:

    public static Duration ofSeconds(long sec_val);
    public static Duration ofSeconds(long sec_val, long nanos_adjust);

  • ofSeconds() method is available in java.time package.

    ofSeconds()方法在java.time包中可用。

  • ofSeconds(long sec_val) method is used to generate a Duration that represents the given seconds.

    ofSeconds(long sec_val)方法用于生成表示给定秒数的Duration。

  • ofSeconds(long sec_val, long nanos_adjust) method is used to generate a Duration that represents the given seconds and adjust in the given nanoseconds if exceeds.

    ofSeconds(long sec_val,long nanos_adjust)方法用于生成表示给定秒数的Duration,如果超过则以给定的纳秒数进行调整。

  • These methods may throw an exception at the time of representing duration in seconds.

    这些方法在以秒为单位表示持续时间时可能会引发异常。

    ArithmeticException: This exception may throw when the length of this Duration is less than the given seconds.

    ArithmeticException :当此Duration的长度小于给定的秒数时,可能引发此异常。

  • These are static methods and it is accessible with the class name and if we try to access these methods with the class object then we will not get an error.

    这些是静态方法,可通过类名进行访问,如果尝试使用类对象访问这些方法,则不会出错。

Parameter(s):

参数:

  • In the first case, ofSeconds(long sec_val),

    在第一种情况下, ofSeconds(long sec_val)

    • long sec_val – represents the number of seconds of the returned Duration.
    • long sec_val –表示返回的持续时间的秒数。
  • In the first case, ofSeconds(long sec_val, long nanos_adjust),

    在第一种情况下, ofSeconds(long sec_val,long nanos_adjust)

    • long sec_val – represents the number of seconds of the returned Duration.
    • long sec_val –表示返回的持续时间的秒数。
    • long nanos_adjust – represents the nanoseconds adjust to the given number of seconds.
    • long nanos_adjust –表示将纳秒调整为给定的秒数。

Return value:

返回值:

In both the cases, the return type of the method is Duration,

在这两种情况下,方法的返回类型均为Duration 。

  • In the first case, it returns the Duration that represents the given number of seconds.

    在第一种情况下,它返回表示给定秒数的持续时间。

  • In the second case, it returns the Duration that represents the given seconds and adjusts in the given nanoseconds.

    在第二种情况下,它返回表示给定秒数的持续时间,并以给定的纳秒为单位进行调整。

Example:

例:

// Java program to demonstrate the example 
// of ofSeconds() method of Duration

import java.time.*;

public class OfSecondsDuration {
    public static void main(String args[]) {
        long sec = 10;
        long nanos_adjust = 20000;

        // returns the Duration that holds the 
        // value of the given seconds
        Duration du_sec = Duration.ofSeconds(sec);

        // Display du_sec
        System.out.println("Duration.ofSeconds(sec): " + du_sec);

        // returns the Duration that holds the 
        // value of the given seconds and adjusted 
        // in nanoseconds
        du_sec = Duration.ofSeconds(sec, nanos_adjust);

        // Display du_sec
        System.out.println("Duration.ofSeconds(sec,nanos_adjust): " + du_sec);
    }
}

Output

输出量

Duration.ofSeconds(sec): PT10S
Duration.ofSeconds(sec,nanos_adjust): PT10.00002S


翻译自: https://www.includehelp.com/java/duration-ofseconds-method-with-example.aspx

duration java

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值