package cn.iocoder.yudao.framework.common.util.date; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.date.DatePattern; import cn.hutool.core.date.LocalDateTimeUtil; import cn.hutool.core.lang.Assert; import cn.hutool.core.util.StrUtil; import cn.iocoder.yudao.framework.common.enums.DateIntervalEnum; import java.sql.Timestamp; import java.time.*; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; import java.time.temporal.ChronoUnit; import java.time.temporal.TemporalAdjusters; import java.util.ArrayList; import java.util.List; import java.util.TimeZone; import static cn.hutool.core.date.DatePattern.*; /** * 时间工具类,用于 {@link LocalDateTime} * * @author 芋道源码 */ public class LocalDateTimeUtils { /** * 空的 LocalDateTime 对象,主要用于 DB 唯一索引的默认值 */ 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(); /** * 解析时间 * * 相比 {@link LocalDateTimeUtil#parse(CharSequence)} 方法来说,会尽量去解析,直到成功 * * @param time 时间 * @return 时间字符串 */ public static LocalDateTime parse(String time) { try { return LocalDateTimeUtil.parse(time, DatePattern.NORM_DATE_PATTERN); } catch (DateTimeParseException e) { return LocalDateTimeUtil.parse(time); } } public static LocalDateTime addTime(Duration duration) { return LocalDateTime.now().plus(duration); } public static LocalDateTime minusTime(Duration duration) { return LocalDateTime.now().minus(duration); } public static boolean beforeNow(LocalDateTime date) { return date.isBefore(LocalDateTime.now()); } 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); } /** * 创建指定时间 * * @param year 年 * @param month 月 * @param day 日 * @return 指定时间 */ public static LocalDateTime buildTime(int year, int month, int day) { return LocalDateTime.of(year, month, day, 0, 0, 0); } public static LocalDateTime[] buildBetweenTime(int year1, int month1, int day1, int year2, int month2, int day2) { return new LocalDateTime[]{buildTime(year1, month1, day1), buildTime(year2, month2, day2)}; } /** * 判指定断时间,是否在该时间范围内 * * @param startTime 开始时间 * @param endTime 结束时间 * @param time 指定时间 * @return 是否 */ public static boolean isBetween(LocalDateTime startTime, LocalDateTime endTime, Timestamp time) { if (startTime == null || endTime == null || time == null) { return false; } return LocalDateTimeUtil.isIn(LocalDateTimeUtil.of(time), startTime, endTime); } /** * 判指定断时间,是否在该时间范围内 * * @param startTime 开始时间 * @param endTime 结束时间 * @param time 指定时间 * @return 是否 */ public static boolean isBetween(LocalDateTime startTime, LocalDateTime endTime, String time) { if (startTime == null || endTime == null || time == null) { return false; } return LocalDateTimeUtil.isIn(parse(time), startTime, endTime); } /** * 判断当前时间是否在该时间范围内 * * @param startTime 开始时间 * @param endTime 结束时间 * @return 是否 */ public static boolean isBetween(LocalDateTime startTime, LocalDateTime endTime) { if (startTime == null || endTime == null) { return false; } return LocalDateTimeUtil.isIn(LocalDateTime.now(), startTime, endTime); } /** * 判断当前时间是否在该时间范围内 * * @param startTime 开始时间 * @param endTime 结束时间 * @return 是否 */ public static boolean isBetween(String startTime, String endTime) { if (startTime == null || endTime == null) { return false; } LocalDate nowDate = LocalDate.now(); return LocalDateTimeUtil.isIn(LocalDateTime.now(), LocalDateTime.of(nowDate, LocalTime.parse(startTime)), LocalDateTime.of(nowDate, LocalTime.parse(endTime))); } /** * 判断时间段是否重叠 * * @param startTime1 开始 time1 * @param endTime1 结束 time1 * @param startTime2 开始 time2 * @param endTime2 结束 time2 * @return 重叠:true 不重叠:false */ public static boolean isOverlap(LocalTime startTime1, LocalTime endTime1, LocalTime startTime2, LocalTime endTime2) { LocalDate nowDate = LocalDate.now(); return LocalDateTimeUtil.isOverlap(LocalDateTime.of(nowDate, startTime1), LocalDateTime.of(nowDate, endTime1), LocalDateTime.of(nowDate, startTime2), LocalDateTime.of(nowDate, endTime2)); } /** * 获取指定日期所在的月份的开始时间 * 例如:2023-09-30 00:00:00,000 * * @param date 日期 * @return 月份的开始时间 */ public static LocalDateTime beginOfMonth(LocalDateTime date) { return date.with(TemporalAdjusters.firstDayOfMonth()).with(LocalTime.MIN); } /** * 获取指定日期所在的月份的最后时间 * 例如:2023-09-30 23:59:59,999 * * @param date 日期 * @return 月份的结束时间 */ public static LocalDateTime endOfMonth(LocalDateTime date) { return date.with(TemporalAdjusters.lastDayOfMonth()).with(LocalTime.MAX); } /** * 获得指定日期所在季度 * * @param date 日期 * @return 所在季度 */ public static int getQuarterOfYear(LocalDateTime date) { return (date.getMonthValue() - 1) / 3 + 1; } /** * 获取指定日期到现在过了几天,如果指定日期在当前日期之后,获取结果为负 * * @param dateTime 日期 * @return 相差天数 */ public static Long between(LocalDateTime dateTime) { return LocalDateTimeUtil.between(dateTime, LocalDateTime.now(), ChronoUnit.DAYS); } /** * 获取今天的开始时间 * * @return 今天 */ public static LocalDateTime getToday() { return LocalDateTimeUtil.beginOfDay(LocalDateTime.now()); } /** * 获取昨天的开始时间 * * @return 昨天 */ public static LocalDateTime getYesterday() { return LocalDateTimeUtil.beginOfDay(LocalDateTime.now().minusDays(1)); } /** * 获取本月的开始时间 * * @return 本月 */ public static LocalDateTime getMonth() { return beginOfMonth(LocalDateTime.now()); } /** * 获取本年的开始时间 * * @return 本年 */ public static LocalDateTime getYear() { return LocalDateTime.now().with(TemporalAdjusters.firstDayOfYear()).with(LocalTime.MIN); } /** * 获取最近 N 天的 0 点时刻序列(升序,含今天) *
* 例:getLatestDays(3) 返回 [前天 00:00, 昨天 00:00, 今天 00:00]
*
* @param days 天数(含今天)
* @return 升序的 LocalDateTime 列表
*/
public static List