shikeying
2024-01-11 3b67e947e36133e2a40eb2737b15ea375e157ea0
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
package com.walker.infrastructure.time;
 
import com.walker.infrastructure.utils.DateUtils;
import com.walker.infrastructure.utils.StringUtils;
 
public class TimeRangeUtils {
 
    /**
     * 返回选择解析过的时间范围,该方法包含了多种范围定义。
     * @param data 时间范围选择参数,参考:{@linkplain TimeConstants}
     * @return
     * @date 2023-06-02
     */
    public static final TimeRange getDateLimit(String data){
        if(StringUtils.isEmpty(data)){
            return null;
        }
        Long startTime = null;
        Long endTime = null;
 
        if(data.equals(TimeConstants.SEARCH_DATE_DAY)){
            startTime = getCurrentDateTimeStart();
            endTime = startTime + 235959;
 
        } else if (data.equals(TimeConstants.SEARCH_DATE_YESTERDAY)) {
            startTime = DateUtils.getAfterLongTime(getCurrentDateTimeStart(), -1);
            endTime = startTime + 235959;
 
        } else if (data.equals(TimeConstants.SEARCH_DATE_LATELY_7)) {
            startTime = DateUtils.getAfterLongTime(getCurrentDateTimeStart(), -6);
//            endTime = getCurrentDateTimeStart() + 235959;
 
        } else if (data.equals(TimeConstants.SEARCH_DATE_LATELY_30)) {
            startTime = DateUtils.getAfterLongTime(getCurrentDateTimeStart(), -30);
//            endTime = getCurrentDateTimeStart() + 235959;
 
        } else if(data.equals(TimeConstants.SEARCH_DATE_WEEK)){
            startTime = DateUtils.getCurrentWeekStartDay();
            endTime = DateUtils.getAfterLongTime(startTime, 6) + 235959;
 
        } else if (data.equals(TimeConstants.SEARCH_DATE_PRE_WEEK)) {
            startTime = DateUtils.getAfterLongTime(DateUtils.getCurrentWeekStartDay(), -7);
            // 上周结束时间
            endTime = DateUtils.getAfterLongTime(startTime, 6) + 235959;
 
        } else if (data.equals(TimeConstants.SEARCH_DATE_MONTH)) {
            startTime = DateUtils.getCurrentMonthStartDay();
            endTime = DateUtils.getCurrentMonthEndDay();
 
        } else if (data.equals(TimeConstants.SEARCH_DATE_PRE_MONTH)) {
            startTime = DateUtils.getLastMonthStartDay();
            endTime = DateUtils.getLastMonthEndDay();
 
        } else if (data.equals(TimeConstants.SEARCH_DATE_YEAR)) {
            startTime = DateUtils.getCurrentYearStartDay();
            endTime = DateUtils.getCurrentYearEndDay();
 
        } else if (data.equals(TimeConstants.SEARCH_DATE_PRE_YEAR)) {
            startTime = DateUtils.getLastYearStartDay();
            endTime = DateUtils.getLastYearEndDay();
 
        } else {
            // 时间范围,如:2023-06-02,2023-06-05
            String[] timeArray = StringUtils.commaDelimitedListToStringArray(data);
            if(timeArray == null || timeArray.length == 0){
                throw new IllegalArgumentException("日期格式错误,请设置一个范围,如:[ 2023-06-02,2023-06-05 ], error=" + data);
            }
            startTime = DateUtils.toLongDateTime(timeArray[0]);
            if(timeArray.length == 2){
                endTime = DateUtils.toLongDateTime(timeArray[1]) + 235959;
            }
        }
        return new TimeRange(startTime, endTime);
    }
 
    private static final long getCurrentDateTimeStart(){
        long currentDate = DateUtils.getDateNumber(System.currentTimeMillis());
        return currentDate * 1000000;
    }
}