update 重构日期格式化方法,简化调用逻辑

This commit is contained in:
AprilWind 2026-06-11 14:15:29 +08:00
parent 487c413654
commit 52de92ea8c
3 changed files with 5 additions and 21 deletions

View File

@ -4,7 +4,6 @@ import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.BetweenFormatter;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.dromara.common.core.exception.ServiceException;
@ -27,8 +26,8 @@ public class DateUtils extends DateUtil {
* @param end 结束时间(支持 Date/LocalDateTime)
* @return 时分秒格式时间差
*/
public static String formatBetweenBySecond(Object start, Object end) {
return formatTimeBetween(start, end, BetweenFormatter.Level.SECOND);
public static String formatBetween(Object start, Object end) {
return formatBetween(start, end, BetweenFormatter.Level.SECOND);
}
/**
@ -39,18 +38,10 @@ public class DateUtils extends DateUtil {
* @param level 精度级别
* @return 格式化时长
*/
public static String formatTimeBetween(Object startDate, Object endDate, BetweenFormatter.Level level) {
// 非空校验
Assert.notNull(startDate, "开始时间不能为空");
Assert.notNull(endDate, "结束时间不能为空");
Assert.notNull(level, "时间精度级别不能为空");
public static String formatBetween(Object startDate, Object endDate, BetweenFormatter.Level level) {
// 统一转为Date并校验格式合法性
Date start = Convert.toDate(startDate);
Date end = Convert.toDate(endDate);
Assert.notNull(start, "开始时间格式错误,无法解析为时间");
Assert.notNull(end, "结束时间格式错误,无法解析为时间");
long diffMillis = Math.abs(end.getTime() - start.getTime());
return formatBetween(diffMillis, level);
}
@ -64,16 +55,9 @@ public class DateUtils extends DateUtil {
* @param unit 时间单位
*/
public static void validateDateRange(Object startDate, Object endDate, int maxValue, TimeUnit unit) {
// 基础非空校验
Assert.notNull(startDate, "开始日期不能为空");
Assert.notNull(endDate, "结束日期不能为空");
Assert.notNull(unit, "时间单位不能为空");
// 统一转换并校验时间合法性
Date start = Convert.toDate(startDate);
Date end = Convert.toDate(endDate);
Assert.notNull(start, "开始日期格式错误,无法解析为时间");
Assert.notNull(end, "结束日期格式错误,无法解析为时间");
// 校验结束时间不能早于开始时间
if (end.before(start)) {

View File

@ -239,7 +239,7 @@ public class FlowHisTaskVo implements Serializable {
private void updateRunDuration() {
// 如果创建时间和更新时间均不为空计算它们之间的时长
if (this.updateTime != null && this.createTime != null) {
this.runDuration = DateUtils.formatBetweenBySecond(this.updateTime, this.createTime);
this.runDuration = DateUtils.formatBetween(this.updateTime, this.createTime);
}
}

View File

@ -202,7 +202,7 @@ public class FlwChartExtServiceImpl implements ChartExtService {
// 添加具体信息项账号耗时时间
info.add(buildInfoItem("用户账号", userDTO.getUserName()));
info.add(buildInfoItem("审批状态", dictType.get(task.getFlowStatus())));
info.add(buildInfoItem("审批耗时", DateUtils.formatBetweenBySecond(task.getUpdateTime(), task.getCreateTime())));
info.add(buildInfoItem("审批耗时", DateUtils.formatBetween(task.getUpdateTime(), task.getCreateTime())));
info.add(buildInfoItem("办理时间", DateUtils.formatDateTime(task.getUpdateTime())));
}
}