public static void main(String[] args) throws InterruptedException {
LocalDateTime offWorkTime = LocalDateTime.now().withHour(17).withMinute(30).withSecond(0);
for (; ; ) {
Thread.sleep(1000);
LocalDateTime currentTime = LocalDateTime.now();
LocalDateTime tempDateTime = LocalDateTime.from(currentTime);
long days = tempDateTime.until(offWorkTime, ChronoUnit.DAYS);
tempDateTime = tempDateTime.plusDays(days);
long hours = tempDateTime.until(offWorkTime, ChronoUnit.HOURS);
tempDateTime = tempDateTime.plusHours(hours);
long minutes = tempDateTime.until(offWorkTime, ChronoUnit.MINUTES);
tempDateTime = tempDateTime.plusMinutes(minutes);
long seconds = tempDateTime.until(offWorkTime, ChronoUnit.SECONDS);
System.err.println("距离下班还有:" + days + " 天 " + hours + " 小时 " + minutes + " 分 " + seconds + " 秒。");
if (currentTime.isAfter(offWorkTime)) {
return;
}
}
}
使用LocalDateTime的until方法计算相差的天,时,分,秒
最新推荐文章于 2024-07-17 16:57:10 发布