计算当前时间到当天24点的毫秒

  1. /**
  2. * 获取当天期剩余的毫秒数
  3. *
  4. * @param currentDateTime {@code LocalDateTime} 当前日期时间
  5. * @return {@code long } 剩余毫秒数
  6. */
  7. public static long getRemainSecondsOneDay(LocalDateTime currentDateTime) {
  8. //使用plusDays加传入的时间加1天,将时分秒设置成0
  9. LocalDateTime midnight = currentDateTime.plusDays(1).withHour(0).withMinute(0).withSecond(0).withNano(0);
  10. //使用ChronoUnit.SECONDS.between方法,传入两个LocalDateTime对象即可得到相差的秒数
  11. return ChronoUnit.SECONDS.between(currentDateTime, midnight);
  12. }