java.time.Duration.ofSeconds() 方法示例



說明

java.time.Duration.ofSeconds(long seconds) 方法獲取表示秒數和納秒調整時長的持續時間。

宣告

以下是 java.time.Duration.ofSeconds(long seconds) 方法的宣告。

public static Duration ofSeconds(long seconds, long nanoAdjustment)

引數

  • seconds - 秒數,為正數或負數。

  • nanoAdjustment - 秒數的納秒調整,為正數或負數。

返回值

Duration,非空。

異常

ArithmeticException - 如果調整導致秒數超過 Duration 的容量。

示例

以下示例顯示 java.time.Duration.ofSeconds(long seconds, long nanoAdjustment) 方法的用法。

package com.tutorialspoint;

import java.time.Duration;

public class DurationDemo {
   public static void main(String[] args) {

      Duration duration = Duration.ofSeconds(2);
      Duration duration1 = duration.plus(Duration.ofSeconds(5));
      System.out.println(duration1.toMillis());
   }
}

讓我們編譯並執行上述程式,這將產生以下結果 -

7000
廣告
© . All rights reserved.