取下一个一分钟和五分钟的秒数

carlors / 2023-08-29 / 原文

//下一个1分钟的秒数

LocalDateTime currentTime = LocalDateTime.now();
LocalDateTime nextMinute = currentTime.plusMinutes(1).truncatedTo(ChronoUnit.MINUTES);
long between = ChronoUnit.SECONDS.between(currentTime, nextMinute);



//下一个五分钟的秒数
LocalDateTime currentTime = LocalDateTime.now();

LocalDateTime nextFiveMinute = currentTime.plusMinutes(5 - (currentTime.getMinute() % 5))
.truncatedTo(ChronoUnit.MINUTES);
long remainingTimeInSeconds = ChronoUnit.SECONDS.between(currentTime, nextFiveMinute);

System.out.println("当前时间: " + currentTime);
System.out.println("下一个五分钟的时间: " + nextFiveMinute);
System.out.println("剩余时间(秒): " + remainingTimeInSeconds);