Java 中的 Instant until() 方法
可以在 Java 中使用 Instant 類的 until() 方法計算兩個即時物件之間的時間。此方法需要兩個引數,即結束時間和用於測量時間的計時單位。它會返回兩個即時物件之間的時間。
演示此問題的程式如下所示 −
示例
import java.time.*;
import java.time.temporal.ChronoUnit;
public class Demo {
public static void main(String[] args) {
Instant i1 = Instant.parse("2019-01-13T11:45:13.00Z");
Instant i2 = Instant.parse("2019-01-13T15:30:12.00Z");
long time = i1.until(i2, ChronoUnit.HOURS);
System.out.println("Instant object i1 is: " + i1);
System.out.println("Instant object i2 is: " + i2);
System.out.println("
The time between two instant objects in hours is: " + time);
}
}輸出
Instant object i1 is: 2019-01-13T11:45:13Z Instant object i2 is: 2019-01-13T15:30:12Z The time between two instant objects in hours is: 3
現在讓我們來了解一下上面的程式。
顯示了兩個即時物件。然後使用 until() 方法計算這兩個即時物件之間的時間並顯示出來。演示此問題的程式碼片段如下所示 −
Instant i1 = Instant.parse("2019-01-13T11:45:13.00Z");
Instant i2 = Instant.parse("2019-01-13T15:30:12.00Z");
long time = i1.until(i2, ChronoUnit.HOURS);
System.out.println("Instant object i1 is: " + i1);
System.out.println("Instant object i2 is: " + i2);
System.out.println("
The time between two instant objects in hours is: " + time);
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP