获取两个时间瞬间之间的毫秒数的 Java 程序
首先,创建两个时刻 -
Instant time1 = Instant.now();Instant time2 = time1.plus(5, ChronoUnit.HOURS).plus(50, ChronoUnit.SECONDS);
使用between()得到两个时刻之间的毫秒-
Duration duration = Duration.ofSeconds(13);Instant i = time1.plus(duration);
System.out.println("Milliseconds between two time instants = "+ChronoUnit.MILLIS.between(time1, time2));
示例
import java.time.Duration;输出结果import java.time.Instant;
import java.time.temporal.ChronoUnit;
public class Demo {
public static void main(String[] args) {
Instant time1 = Instant.now();
Instant time2 = time1.plus(5, ChronoUnit.HOURS).plus(50, ChronoUnit.SECONDS);
Duration duration = Duration.ofSeconds(13);
Instant i = time1.plus(duration);
System.out.println("Milliseconds between two time instants =
"+ChronoUnit.MILLIS.between(time1, time2));
}
}
Milliseconds between two time instants = 18050000
以上是 获取两个时间瞬间之间的毫秒数的 Java 程序 的全部内容, 来源链接: utcz.com/z/335616.html