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



說明

java.time.Duration.ofSeconds(long seconds) 方法獲取一個表示標準秒數的持續時間。

宣告

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

public static Duration ofSeconds(long seconds)

引數

秒數 - 秒數,可以是正數或負數。

返回值

持續時間,非空。

異常

算術異常 - 如果輸入的秒數超出持續時間容量。

示例

以下示例展示了 java.time.Duration.ofSeconds(long seconds) 方法的用法。

package com.tutorialspoint;

import java.time.Duration;

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

      Duration duration = Duration.ofSeconds(2);
      System.out.println(duration.getSeconds());      
   }
}

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

2
廣告
© . All rights reserved.