xiangpei
2025-04-18 ccadf9480d4e6a9dcc227a2a0b1f9ae0612e36fd
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package com.monkeylessey.utils;
 
import com.monkeylessey.enums.WeekEnum;
import com.monkeylessey.exception.BaseException;
import org.springframework.util.StringUtils;
 
import javax.annotation.Nullable;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
 
/**
 * 日期工具类
 *
 * @author xiangpei
 * @date 2023/7/7
 */
public class DateUtil {
 
    private static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
 
 
    /**
     * 获取某天的开始时间
     *
     * @param date
     * @return 2023-01-01 00:00:00
     */
    public static Date getDayStart(@Nullable Date date) {
        if (Objects.isNull(date)) {
            date = new Date();
        }
        LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.of("GMT+8"));
        LocalDateTime of = LocalDateTime.of(localDateTime.getYear(), localDateTime.getMonth(), localDateTime.getDayOfMonth(), 0, 0, 0);
        return Timestamp.valueOf(of);
    }
 
    /**
     * 获取某天的结束时间
     *
     * @param date
     * @return 2023-01-01 23:59:59
     */
    public static Date getDayEnd(@Nullable Date date) {
        if (Objects.isNull(date)) {
            date = new Date();
        }
        LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
        LocalDateTime of = LocalDateTime.of(localDateTime.getYear(), localDateTime.getMonth(), localDateTime.getDayOfMonth(), 23, 59, 59);
        return Timestamp.valueOf(of);
    }
 
    /**
     * 获取某月的第一天
     *
     * @param date 2023-01-05
     * @return 2023-01-01
     */
    public static Date getMonthStart(@Nullable Date date) {
        if (Objects.isNull(date)) {
            date = new Date();
        }
        LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
        LocalDateTime start = localDateTime.with(TemporalAdjusters.firstDayOfMonth());
        return Timestamp.valueOf(start);
    }
 
    /**
     * 获取某月的最后一天
     *
     * @param date
     * @return
     */
    public static Date getMonthEnd(@Nullable Date date) {
        if (Objects.isNull(date)) {
            date = new Date();
        }
        LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
        LocalDateTime end = localDateTime.with(TemporalAdjusters.lastDayOfMonth());
        return Timestamp.valueOf(end);
    }
 
    /**
     * 获取某年的最后一天
     *
     * @param date
     * @return
     */
    public static Date getYearEnd(@Nullable Date date) {
        if (Objects.isNull(date)) {
            date = new Date();
        }
        LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
        LocalDateTime end = localDateTime.with(TemporalAdjusters.lastDayOfYear());
        return Timestamp.valueOf(end);
    }
 
    /**
     * 获取某年的第一天
     *
     * @param date
     * @return
     */
    public static Date getYearStart(@Nullable Date date) {
        if (Objects.isNull(date)) {
            date = new Date();
        }
        LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
        LocalDateTime end = localDateTime.with(TemporalAdjusters.firstDayOfYear());
        return Timestamp.valueOf(end);
    }
 
    /**
     * 获取某天是星期几
     *
     * @param date
     */
    public static WeekEnum getWeek(@Nullable Date date) {
        if (Objects.isNull(date)) {
            date = new Date();
        }
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+8"));
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        for (WeekEnum weekDay : WeekEnum.values()) {
            if (weekDay.getCode() == dayOfWeek) {
                return weekDay;
            }
        }
        return null;
    }
 
    /**
     * 格式化日期
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String formatDate(@Nullable Date date, @Nullable String pattern) {
        if (Objects.isNull(date)) {
            date = new Date();
        }
        if (!StringUtils.hasText(pattern)) {
            pattern = DEFAULT_DATE_FORMAT;
        }
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(date);
    }
 
    /**
     * 获取时间段内的每一天 每一月 每一年
     *
     * @param startDate
     * @param endDate
     * @param dateType
     * @return
     */
    public static List<String> getEveryItem(Date startDate, Date endDate, DateType dateType) throws ParseException {
        if (Objects.isNull(startDate) || Objects.isNull(endDate)) {
            throw new BaseException("传入时间不能为空");
        }
        checkTimeSize(startDate, endDate);
        int diffNumber = 1;
        List<String> result = new ArrayList<>(10);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);
        SimpleDateFormat format;
        switch (dateType) {
            case DAY:
                format = new SimpleDateFormat("yyyy-MM-dd");
                diffNumber = (int) Math.abs((format.parse(format.format(startDate)).getTime() - format.parse(format.format(endDate)).getTime()) / (1000 * 3600 * 24)) + 1;
                getEveryResult(diffNumber, format, calendar, Calendar.DAY_OF_MONTH, result);
                break;
            case MONTH:
                format = new SimpleDateFormat("yyyy-MM");
                Period between = Period.between(dateToLocal(format.parse(format.format(startDate))), dateToLocal(format.parse(format.format(endDate))));
                diffNumber = between.getMonths() + between.getYears() * 12 + 1;
                getEveryResult(diffNumber, format, calendar, Calendar.MONTH, result);
                break;
            case YEAR:
                format = new SimpleDateFormat("yyyy");
                diffNumber = Period.between(dateToLocal(format.parse(format.format(startDate))), dateToLocal(format.parse(format.format(endDate)))).getYears() + 1;
                getEveryResult(diffNumber, format, calendar, Calendar.YEAR, result);
            default:
                break;
        }
        return result;
    }
 
    /**
     * 获取时间范围内的天、月、年
     *
     * @param diffNumber
     * @param format
     * @param calendar
     * @param calendarDateType
     * @param result
     */
    private static void getEveryResult(int diffNumber, SimpleDateFormat format, Calendar calendar, int calendarDateType, List<String> result) {
        for (int i = 0; i < diffNumber; i++) {
            result.add(format.format(calendar.getTime()));
            calendar.add(calendarDateType, 1);
        }
    }
 
 
    /**
     * 检验开始时间不能大于结束时间
     *
     * @param startDate
     * @param endDate
     */
    public static void checkTimeSize(Date startDate, Date endDate) {
        if (startDate.after(endDate)) {
            throw new BaseException("开始时间不能大于结束时间");
        }
    }
 
    /**
     * date 转 localDate
     *
     * @param date
     * @return
     */
    public static LocalDate dateToLocal(Date date) {
        if (Objects.isNull(date)) {
            throw new BaseException("传入时间不能为空");
        }
        Instant instant = date.toInstant();
        return instant.atZone(ZoneId.systemDefault()).toLocalDate();
    }
 
 
    public enum DateType {
 
        DAY("获取时间段的每一天"),
        MONTH("获取时间段的每一个月"),
        YEAR("获取时间段的每一年");
 
        private final String desc;
 
        DateType(String desc) {
            this.desc = desc;
        }
    }
}