Files
tr-agent-client/src/main/java/com/tongran/agent/client/utils/RoundMinutes.java
T
2025-09-10 15:36:49 +08:00

53 lines
1.8 KiB
Java

package com.tongran.agent.client.utils;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
public class RoundMinutes {
public static long convertMillisToRoundedSeconds(long millisTimestamp) {
// 1. 先将毫秒时间戳四舍五入到秒
long roundedSeconds = Math.round(millisTimestamp / 1000.0);
// 2. 转换为LocalDateTime进行分钟调整
LocalDateTime dateTime = LocalDateTime.ofInstant(
Instant.ofEpochSecond(roundedSeconds),
ZoneId.systemDefault()
);
// 3. 将分钟四舍五入到5分钟间隔
int minute = dateTime.getMinute();
int roundedMinute = (int) (Math.round(minute / 5.0) * 5);
// 4. 调整时间
LocalDateTime adjustedDateTime = dateTime
.withMinute(roundedMinute % 60)
.withSecond(0)
.withNano(0);
// 处理进位
if (roundedMinute == 60) {
adjustedDateTime = adjustedDateTime.plusHours(1);
}
// 5. 转换回时间戳
return adjustedDateTime.atZone(ZoneId.systemDefault()).toEpochSecond();
}
// public static void main(String[] args) {
// long millisTimestamp = System.currentTimeMillis();
// System.out.println("原始13位时间戳: " + millisTimestamp);
// System.out.println("原始时间: " + new java.util.Date(millisTimestamp));
//
// long result = convertMillisToRoundedSeconds(millisTimestamp);
// System.out.println("调整后10位时间戳: " + result);
// System.out.println("调整后时间: " + new java.util.Date(result * 1000));
// }
//
public static void main(String[] args) {
long timestamp = 1757471699499L;
long time = Math.round(timestamp / 1000.0);
System.out.println(time);
}
}