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