如何獲取 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
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C #
MongoDB
MySQL
Javascript
PHP