我要计算两个LocalDateTime对象之间的时间差(以毫秒为单位):
LocalDateTime startDate = LocalDateTime.of(2017, 11, 22, 21, 30, 30, 250);
LocalDateTime endDate = LocalDateTime.of(2017, 11, 22, 21, 30, 30, 252);
long diff = ChronoUnit.MILLIS.between(startDate, endDate)
不过,diff的值并不像我所期望的那样是2,而是0。发生什么事?
最佳答案:
LocalDateTime startDate = LocalDateTime.of(2017, 11, 22, 21, 30, 30, 250);
LocalDateTime endDate = LocalDateTime.of(2017, 11, 22, 21, 30, 30, 252);
long diff = ChronoUnit.NANOS.between(startDate, endDate);
System.out.println(diff);
产量:
2
我认为既然你在比较millis,那么diff就被舍入了。