如何獲取 Java 中兩個 Instant 時間戳之間的秒數和分鐘數


以下是兩個 Instant 時間戳

Instant one = Instant.ofEpochSecond(1355836728);
Instant two = Instant.ofEpochSecond(1355866935);

獲取兩個 Instant 之間的持續時間

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

現在,獲取兩個時間戳之間的秒數

long seconds = res.getSeconds();

現在,獲取兩個時間戳之間的分鐘數

long minutes = res.abs().toMinutes();

示例

import java.time.Duration;
import java.time.Instant;
public class Demo {
   public static void main(String[] args) {
      Instant one = Instant.ofEpochSecond(1355836728);
      Instant two = Instant.ofEpochSecond(1355866935);
      Duration res = Duration.between(one, two);
      System.out.println(res);
      long seconds = res.getSeconds();
      System.out.println("Seconds between Durations = "+seconds);
      long minutes = res.abs().toMinutes();
      System.out.println("Minutes between Durations = "+minutes);
   }
}

輸出

PT8H23M27S
Seconds between Durations = 30207
Minutes between Durations = 503

更新時間: 2019-07-30

3K+ 檢視次數

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.