2026-07-01 6b5f7c66fc40d7f6099d561e31a34fbd50dd20d3
yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/date/LocalDateTimeUtils.java
@@ -3,7 +3,6 @@
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.date.TemporalAccessorUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.enums.DateIntervalEnum;
@@ -16,6 +15,7 @@
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;
import java.util.TimeZone;
import static cn.hutool.core.date.DatePattern.*;
@@ -32,6 +32,11 @@
    public static LocalDateTime EMPTY = buildTime(1970, 1, 1);
    public static DateTimeFormatter UTC_MS_WITH_XXX_OFFSET_FORMATTER = createFormatter(UTC_MS_WITH_XXX_OFFSET_PATTERN);
    /**
     * 默认时区
     */
    private static final ZoneId DEFAULT_ZONE_ID = TimeZone.getTimeZone(DateUtils.TIME_ZONE_DEFAULT).toZoneId();
    /**
     * 解析时间
@@ -63,6 +68,27 @@
    public static boolean afterNow(LocalDateTime date) {
        return date.isAfter(LocalDateTime.now());
    }
    /**
     * 将 Unix 秒时间戳转换为默认时区的本地时间
     *
     * @param epochSecond Unix 秒时间戳
     * @return 本地时间
     */
    public static LocalDateTime ofEpochSecond(long epochSecond) {
        return ofEpochSecond(epochSecond, DEFAULT_ZONE_ID);
    }
    /**
     * 将 Unix 秒时间戳转换为指定时区的本地时间
     *
     * @param epochSecond Unix 秒时间戳
     * @param zoneId 时区编号
     * @return 本地时间
     */
    public static LocalDateTime ofEpochSecond(long epochSecond, ZoneId zoneId) {
        return LocalDateTime.ofInstant(Instant.ofEpochSecond(epochSecond), zoneId);
    }
    /**
@@ -236,6 +262,23 @@
        return LocalDateTime.now().with(TemporalAdjusters.firstDayOfYear()).with(LocalTime.MIN);
    }
    /**
     * 获取最近 N 天的 0 点时刻序列(升序,含今天)
     * <p>
     * 例:getLatestDays(3) 返回 [前天 00:00, 昨天 00:00, 今天 00:00]
     *
     * @param days 天数(含今天)
     * @return 升序的 LocalDateTime 列表
     */
    public static List<LocalDateTime> getLatestDays(int days) {
        LocalDateTime today = getToday();
        List<LocalDateTime> dates = new ArrayList<>(days);
        for (int i = days - 1; i >= 0; i--) {
            dates.add(today.minusDays(i));
        }
        return dates;
    }
    public static List<LocalDateTime[]> getDateRangeList(LocalDateTime startTime,
                                                         LocalDateTime endTime,
                                                         Integer interval) {
@@ -376,7 +419,18 @@
     * @throws DateTimeException 如果转换过程中发生时间超出范围或其他时间处理异常
     */
    public static Long toEpochSecond(LocalDateTime sourceDateTime) {
        return TemporalAccessorUtil.toInstant(sourceDateTime).getEpochSecond();
        return toEpochSecond(sourceDateTime, DEFAULT_ZONE_ID);
    }
    /**
     * 将给定的 {@link LocalDateTime} 按指定时区转换为自 Unix 纪元时间(1970-01-01T00:00:00Z)以来的秒数。
     *
     * @param sourceDateTime 需要转换的本地日期时间,不能为空
     * @param zoneId 时区编号
     * @return 自 1970-01-01T00:00:00Z 起的秒数(epoch second)
     */
    public static Long toEpochSecond(LocalDateTime sourceDateTime, ZoneId zoneId) {
        return sourceDateTime.atZone(zoneId).toEpochSecond();
    }
}