首先,创建两个时刻 -
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