package org.sxkj.common.utils; import org.sxkj.common.enums.DateEnum; import org.sxkj.common.model.TimeRange; import java.time.*; import java.time.temporal.TemporalAdjusters; import java.util.Optional; /** * 根据枚举获取开始时间-结束时间 * 修改说明:所有截止时间都设置为当前时间 */ public class TimeRangeUtils { public static TimeRange getTimeRange(DateEnum type) { LocalDate today = LocalDate.now(); LocalTime startOfDay = LocalTime.MIN; // 00:00:00 LocalDateTime now = LocalDateTime.now(); switch (type) { case TODAY: return new TimeRange( LocalDateTime.of(today, startOfDay), LocalDateTime.of(today, LocalTime.MAX) ); case CURRENT_WEEK: LocalDate startOfWeek = today.with(DayOfWeek.MONDAY); return new TimeRange( LocalDateTime.of(startOfWeek, startOfDay), today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)).atTime(LocalTime.MAX) ); case CURRENT_MONTH: LocalDate startOfMonth = today.withDayOfMonth(1); return new TimeRange( LocalDateTime.of(startOfMonth, startOfDay), LocalDate.now() .with(TemporalAdjusters.lastDayOfMonth()).atTime(LocalTime.MAX) ); case CURRENT_YEAR: LocalDate startOfYear = today.withDayOfYear(1); return new TimeRange( LocalDateTime.of(startOfYear, startOfDay), LocalDate.now().with(TemporalAdjusters.lastDayOfYear()).atTime(LocalTime.MAX) ); case RECENT_YEAR: LocalDateTime startOfYearBefore = now.plusYears(-1); return new TimeRange( startOfYearBefore, now ); case MONTHS: startOfYear = today.withDayOfYear(1); return new TimeRange( LocalDateTime.of(startOfYear, startOfDay), LocalDate.now().with(TemporalAdjusters.lastDayOfYear()).atTime(LocalTime.MAX) ); default: return null; } } /** * 获取查询时间范围 * @param type * @param startTime * @return */ public static TimeRange getTimeRangeByStartTimeTwo(DateEnum type,LocalDateTime startTime) { LocalDate today = Optional.ofNullable(startTime).orElse(LocalDateTime.now()).toLocalDate(); LocalTime startOfDay = LocalTime.MIN; // 00:00:00 switch (type) { case DAY: // 如果是日,取这个月开始时间 LocalDate startOfMonth = today.withDayOfMonth(1); return new TimeRange( LocalDateTime.of(startOfMonth, startOfDay), today.atTime(LocalTime.MAX) ); case MONTH: // 如果是月,取今年开始时间 LocalDate startOfYear = today.withDayOfYear(1); return new TimeRange( LocalDateTime.of(startOfYear, startOfDay), today.atTime(LocalTime.MAX) ); case YEAR: LocalDate startOfYearForYear = today.withDayOfYear(1); return new TimeRange( LocalDateTime.of(startOfYearForYear, startOfDay), today.with(TemporalAdjusters.lastDayOfYear()).atTime(LocalTime.MAX) ); case WEEK: LocalDate startOfWeek = today.with(DayOfWeek.MONDAY); return new TimeRange(LocalDateTime.of(startOfWeek, startOfDay), LocalDateTime.of(today, LocalTime.MAX)); default: throw new IllegalArgumentException("Unsupported time range type: " + type); } } /** * 获取查询时间范围 * @param type * @param startTime * @return */ public static TimeRange getTimeRangeByStartTime(DateEnum type,LocalDateTime startTime) { LocalDate today = Optional.ofNullable(startTime).orElse(LocalDateTime.now()).toLocalDate(); LocalTime startOfDay = LocalTime.MIN; // 00:00:00 switch (type) { case DAY: return new TimeRange( LocalDateTime.of(today, startOfDay), LocalDateTime.of(today, LocalTime.MAX) ); case MONTH: LocalDate startOfMonth = today.withDayOfMonth(1); LocalDate endDay = today .with(TemporalAdjusters.lastDayOfMonth()); return new TimeRange( LocalDateTime.of(startOfMonth, startOfDay), endDay.atTime(LocalTime.MAX) ); case YEAR: LocalDate startOfYear = today.withDayOfYear(1); return new TimeRange( LocalDateTime.of(startOfYear, startOfDay), today.with(TemporalAdjusters.lastDayOfYear()).atTime(LocalTime.MAX) ); case WEEK: LocalDate startOfWeek = today.with(DayOfWeek.MONDAY); return new TimeRange(LocalDateTime.of(startOfWeek, startOfDay), LocalDateTime.of(today, LocalTime.MAX)); default: throw new IllegalArgumentException("Unsupported time range type: " + type); } } /** * 获取到1970年时间戳 * @param time 本地日期时间 * @return 时间戳(毫秒) */ public static Long getEpochMilli(LocalDateTime time) { return time.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); } }