工具类-时间优化

This commit is contained in:
qiminbao
2025-09-28 11:00:38 +08:00
parent 72626c2b69
commit 179e29d9a5
@@ -200,15 +200,14 @@ public class AgentUtil {
public static long roundMinutes(){ public static long roundMinutes(){
LocalDateTime now = LocalDateTime.now(); LocalDateTime now = LocalDateTime.now();
// 四舍五入到最近的5分钟 int minute = now.getMinute();
LocalDateTime roundedTime = now int roundedMinute = (int) (Math.round(minute / 5.0) * 5);
.truncatedTo(ChronoUnit.MINUTES) // 先去掉秒和纳秒
.withMinute((int) (Math.round(now.getMinute() / 5.0) * 5)) LocalDateTime roundedTime ;
.withSecond(0) if (roundedMinute == 60) {
.withNano(0); roundedTime = now.plusHours(1).withMinute(0).withSecond(0).withNano(0);
// 处理分钟进位的情况 } else {
if (roundedTime.getMinute() == 60) { roundedTime = now.withMinute(roundedMinute).withSecond(0).withNano(0);
roundedTime = roundedTime.plusHours(1).withMinute(0);
} }
// 转换为10位时间戳 // 转换为10位时间戳
long timestamp = roundedTime.atZone(ZoneId.systemDefault()).toEpochSecond(); long timestamp = roundedTime.atZone(ZoneId.systemDefault()).toEpochSecond();