吉安感知网项目-后端
xiebin
2026-01-06 d207a86cdf1ab52ef8cb7cd83bad8fceab8038cf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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();
    }
}