假设以下是我们想要毫秒差的两个日期-
LocalDateTime dateOne = LocalDateTime.now(); LocalDateTime dateTwo = LocalDateTime.of(2019, 4, 10, 11, 20);
现在,使用MILLIS.between获得上述两个日期之间的毫秒数-
long res = MILLIS.between(dateOne, dateTwo);
import java.time.LocalDateTime; import static java.time.temporal.ChronoUnit.MILLIS; public class Demo { public static void main(String[] argv) { LocalDateTime dateOne = LocalDateTime.now(); LocalDateTime dateTwo = LocalDateTime.of(2019, 4, 10, 11, 20); System.out.printf("Date One = "+dateOne); System.out.printf("\nDate Two = "+dateTwo); long res = MILLIS.between(dateOne, dateTwo); System.out.println("\nMilliseconds between two dates = " + res); } }
输出结果
Date One = 2019-04-12T11:18:29.654947300 Date Two = 2019-04-10T11:20 Milliseconds between two dates = -172709654