Java程序获取两个时间点之间的分钟数
首先,创建两个时刻 -
Instant time1 = Instant.now();Instant time2 = time1.plus(5, ChronoUnit.HOURS).plus(10, ChronoUnit.MINUTES);
使用between()得到两个时刻之间的分钟数-
Duration duration = Duration.ofSeconds(13);Instant i = time1.plus(duration);
System.out.println("Minutes between two time instants = "+Duration.between(time1, time2).toMinutes());
示例
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(10, ChronoUnit.MINUTES);
Duration duration = Duration.ofSeconds(13);
Instant i = time1.plus(duration);
System.out.println("Minutes between two time instants = "+Duration.between(time1, time2).toMinutes());
}
}
Minutes between two time instants = 310
以上是 Java程序获取两个时间点之间的分钟数 的全部内容, 来源链接: utcz.com/z/317428.html