timestamp 兼容秒和毫秒

This commit is contained in:
2026-04-23 15:04:46 +08:00
parent 464b7e1a0f
commit 3456d8bc70

View File

@ -181,7 +181,17 @@ public class CommUtil {
format = "yyyy-MM-dd HH:mm:ss";
}
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(new Date(Long.valueOf(seconds)));
String timestamp = seconds.trim();
int dotIndex = timestamp.indexOf('.');
if (dotIndex > -1) {
timestamp = timestamp.substring(0, dotIndex);
}
long timeValue = Long.parseLong(timestamp);
// 兼容秒级和毫秒级时间戳10位附近按秒处理13位附近按毫秒处理。
if (Math.abs(timeValue) < 100000000000L) {
timeValue = timeValue * 1000L;
}
return sdf.format(new Date(timeValue));
}
/**
@ -242,4 +252,3 @@ public class CommUtil {