如何用 Java 獲取兩個 Instant 時間戳之間的持續時間


我們首先使用 ofEpochSeconds() 方法設定兩個 Instant,方法使用 1970-01- 01T00:00:00Z 紀元的秒數。

現在獲取上述兩個 Instant 之間的持續時間

Duration res = Duration.between(one, two);

例項

import java.time.Duration;
import java.time.Instant;
public class Demo {
   public static void main(String[] args) {
      Instant one = Instant.ofEpochSecond(1845836728);
      Instant two = Instant.ofEpochSecond(1845866935);
      Duration res = Duration.between(one, two);
      System.out.println(res);
   }
}

輸出

PT8H23M27S

更新日期:30-Jul-2019

19K+ 瀏覽次數

開啟您的 職業生涯

完成課程即可獲得認證

開始
廣告
© . All rights reserved.