提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
使用即时和持续时间类
import java.time.Duration; import java.time.Instant; public class Main {/**来 自 n o w j a v a . c o m - 时代Java**/ public static void main(String[] args) { Instant i1 = Instant.ofEpochSecond(20); Instant i2 = Instant.ofEpochSecond(55); System.out.println("i1:" + i1); System.out.println("i2:" + i2); Duration d1 = Duration.ofSeconds(55); Duration d2 = Duration.ofSeconds(-17); System.out.println("d1:" + d1); System.out.println("d2:" + d2); // Compare instants System.out.println("i1.isBefore(i2):" + i1.isBefore(i2)); System.out.println("i1.isAfter(i2):" + i1.isAfter(i2)); // Add and subtract durations to instants//来自 n o w j a v a . c o m - 时 代 Java Instant i3 = i1.plus(d1); Instant i4 = i2.minus(d2); System.out.println("i1.plus(d1):" + i3);