tj
2025-06-05 2d549a04870d1315868a7cf19952b64e8071e711
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package com.cloudroam.util;
 
import lombok.extern.slf4j.Slf4j;
 
import java.io.File;
import java.nio.file.spi.FileTypeDetector;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
@Slf4j
public class DateUtils {
    public static void main(String[] args) {
//        List<String > days=getCurrentMonthsDays();
//        List<String > days=getCurrentWeekDays();
//        List<String> days=getNextWeekDays();
        /*List<String> days=getDateRangeDays("2023-08-01","2023-08-10");
 
        for(String day:days){
            System.out.println(day);
        }
*/
 
//        System.out.println(formattedTodayDate());
 
//        System.out.println(formattedYesterdayDate());
 
 
//        days=getPreViousWeekDays();
 
//        days=getPreviousMonthsDays();
//
//
//
//        for(String day:days){
//            System.out.println(day);
//        }
 
 
//        System.out.println(stringToLocalDateTime("2023-08-17"));
 
        System.out.println(getCurrentDateCatalog());
 
 
    }
 
 
    /**
     * 获取今天日期
     * @return
     */
    public static String formattedTodayDate(){
        LocalDate currentDate = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String formattedDate = currentDate.format(formatter);
        return formattedDate;
    }
 
    /**
     * 获取昨天日期
     * @return
     */
    public static String formattedYesterdayDate(){
        LocalDate yesterday = LocalDate.now().minusDays(1); // 获取昨天日期
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String formattedYesterday = yesterday.format(formatter);
        return formattedYesterday;
    }
 
 
 
    /**
     * 获取本月日期列表
     * @return
     */
    public static List<String> getPreviousMonthsDays(){
        LocalDate currentDate = LocalDate.now();
 
        // 获取上个月的年月
        YearMonth lastMonth = YearMonth.from(currentDate.minusMonths(1));
 
        // 获取上个月的天数
        int lastMonthDays = lastMonth.lengthOfMonth();
 
        // 存储日期的列表
        List<LocalDate> lastMonthDates = new ArrayList<>();
 
        // 循环遍历从上个月的第一天到最后一天,将日期添加到列表中
        LocalDate date = LocalDate.of(lastMonth.getYear(), lastMonth.getMonth(), 1);
        for (int i = 0; i < lastMonthDays; i++) {
            lastMonthDates.add(date);
            date = date.plusDays(1);
        }
 
        List<String> dateStrList=new ArrayList<>();
        // 打印日期列表
        for (LocalDate d : lastMonthDates) {
            dateStrList.add(d.toString());
        }
        return dateStrList;
    }
 
    /**
     * 获取本月日期列表
     * @return
     */
    public static List<String> getCurrentMonthsDays(){
        // 获取当前年月
        YearMonth currentYearMonth = YearMonth.now();
        int year = currentYearMonth.getYear();
        int month = currentYearMonth.getMonthValue();
 
        // 获取本月的第一天和最后一天
        LocalDate firstDay = LocalDate.of(year, month, 1);
        LocalDate lastDay = currentYearMonth.atEndOfMonth();
 
        // 存储日期的数组
        List<LocalDate> allDates = new ArrayList<>();
 
        // 循环遍历从第一天到最后一天,将日期添加到数组中
        LocalDate currentDate = firstDay;
        while (!currentDate.isAfter(lastDay)) {
            allDates.add(currentDate);
            currentDate = currentDate.plusDays(1);
        }
 
        // 将日期数组转换为数组
        LocalDate[] datesArray = allDates.toArray(new LocalDate[0]);
        List<String> dateStrList=new ArrayList<>();
        for (LocalDate date : datesArray) {
            dateStrList.add(date.toString());
        }
 
        return dateStrList;
    }
 
    /**
     * 获取本周日期列表
     * @return
     */
    public static List<String> getCurrentWeekDays(){
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
 
        // 计算本周的第一天和最后一天
        LocalDate firstDayOfWeek = currentDate.with(DayOfWeek.MONDAY);
        LocalDate lastDayOfWeek = currentDate.with(DayOfWeek.SUNDAY);
 
        // 存储日期的数组
        List<LocalDate> allDates = new ArrayList<>();
 
        // 循环遍历从本周第一天到最后一天,将日期添加到数组中
        LocalDate date = firstDayOfWeek;
        while (!date.isAfter(lastDayOfWeek)) {
            allDates.add(date);
            date = date.plusDays(1);
        }
 
        // 将日期数组转换为数组
        LocalDate[] datesArray = allDates.toArray(new LocalDate[0]);
        List<String> dateStrList=new ArrayList<>();
        // 打印日期数组
        for (LocalDate d : datesArray) {
            dateStrList.add(d.toString());
        }
 
        return dateStrList;
    }
 
    /**
     * 获取上周日期列表
     * @return
     */
    public static List<String> getPreViousWeekDays(){
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
 
        // 计算下周的第一天和最后一天
        LocalDate firstDayOfNextWeek = currentDate.minusWeeks(1).with(TemporalAdjusters.previous(DayOfWeek.MONDAY));
        LocalDate lastDayOfNextWeek = firstDayOfNextWeek.with(DayOfWeek.SUNDAY);
 
        // 存储日期的数组
        List<LocalDate> allDates = new ArrayList<>();
 
        // 循环遍历从下周第一天到最后一天,将日期添加到数组中
        LocalDate date = firstDayOfNextWeek;
        while (!date.isAfter(lastDayOfNextWeek)) {
            allDates.add(date);
            date = date.plusDays(1);
        }
 
        // 将日期数组转换为数组
        LocalDate[] datesArray = allDates.toArray(new LocalDate[0]);
        List<String> dateStrList=new ArrayList<>();
        // 打印日期数组
        for (LocalDate d : datesArray) {
            dateStrList.add(d.toString());
        }
 
        return dateStrList;
    }
    /**
     * 获取下周日期列表
     * @return
     */
    public static List<String> getNextWeekDays(){
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
 
        // 计算下周的第一天和最后一天
        LocalDate firstDayOfNextWeek = currentDate.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
        LocalDate lastDayOfNextWeek = firstDayOfNextWeek.with(DayOfWeek.SUNDAY);
 
        // 存储日期的数组
        List<LocalDate> allDates = new ArrayList<>();
 
        // 循环遍历从下周第一天到最后一天,将日期添加到数组中
        LocalDate date = firstDayOfNextWeek;
        while (!date.isAfter(lastDayOfNextWeek)) {
            allDates.add(date);
            date = date.plusDays(1);
        }
 
        // 将日期数组转换为数组
        LocalDate[] datesArray = allDates.toArray(new LocalDate[0]);
        List<String> dateStrList=new ArrayList<>();
        // 打印日期数组
        for (LocalDate d : datesArray) {
            dateStrList.add(d.toString());
        }
 
        return dateStrList;
    }
 
    public static List<String> getDateRangeDays(String startDateStr,String endDateStr ){
        // 解析开始时间和结束时间字符串为LocalDate对象
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate startDate = LocalDate.parse(startDateStr, formatter);
        LocalDate endDate = LocalDate.parse(endDateStr, formatter);
 
        // 存储日期的数组
        List<LocalDate> dateArray = new ArrayList<>();
 
        // 循环遍历从开始时间到结束时间,将日期添加到数组中
        LocalDate currentDate = startDate;
        while (!currentDate.isAfter(endDate)) {
            dateArray.add(currentDate);
            currentDate = currentDate.plusDays(1);
        }
 
        List<String> dateStrList=new ArrayList<>();
        // 打印日期数组
        for (LocalDate date : dateArray) {
            dateStrList.add(date.toString());
        }
 
        return dateStrList;
    }
 
 
    /**
     * 根据给定的小时数获取当天的指定小时数的时间
     * @param givenHours
     * @return
     */
    public static LocalDateTime getTodayPointHourTime(int givenHours){
        LocalDate currentDate = LocalDate.now();
        // 构造当天的特定时间
        LocalTime specificTime = LocalTime.of(givenHours, 0);
        return LocalDateTime.of(currentDate, specificTime);
    }
 
    public static LocalDateTime stringToLocalDateTime(String dateString){
        DateTimeFormatter formatter=DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate localDate = LocalDate.parse(dateString,formatter);
        // 构造当天的特定时间
        LocalDateTime localDateTime=localDate.atStartOfDay();
        return localDateTime;
    }
 
    public static String dateToStr(Date date){
 
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strDate = sdf.format(date);
        return strDate;
    }
 
 
    public static boolean isWeekend(LocalDate date) {
        DayOfWeek dayOfWeek = date.getDayOfWeek();
        return dayOfWeek == DayOfWeek.SATURDAY || dayOfWeek == DayOfWeek.SUNDAY;
    }
 
    public static String getTodayString(){
        LocalDate today = LocalDate.now();
 
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String formattedDate = today.format(formatter);
 
        return formattedDate;
    }
 
 
    /**
     * 将Date日期的yyyy-MM-dd HH:mm:ss 的格式转成 yyyy-MM-dd的date
     * @param dailyDate yyyy-MM-dd HH:mm:ss 的日期
     * @return  yyyy-MM-dd的date
     */
    public static Date formatDateToDateYYYYMMDD(Date dailyDate){
        // 创建一个 SimpleDateFormat 对象,用于定义日期时间的输出格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
 
        // 使用 SimpleDateFormat 格式化 Date 对象为只包含年月日的字符串
        String formattedDate = sdf.format(dailyDate);
 
 
        // 将格式化后的字符串重新解析为 Date 对象,只包含年月日部分
 
        Date parsedDate = null;
        try {
            parsedDate = sdf.parse(formattedDate);
            return parsedDate;
        } catch (ParseException e) {
           log.error(e.getMessage());
           return null;
        }
 
    }
 
 
    /**
     * 判断一个日期是否在开始时间和结束时间内
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param targetDate
     * @return
     */
    public static boolean isDateInRange(String startTime, String endTime, String targetDate) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate startDate = LocalDate.parse(startTime, formatter);
        LocalDate endDate = LocalDate.parse(endTime, formatter);
        LocalDate target = LocalDate.parse(targetDate, formatter);
 
        return target.isAfter(startDate) && target.isBefore(endDate);
    }
 
    /**
     * 判断一个日期是否在开始时间和结束时间内
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param targetDate
     * @return
     */
    public static boolean isDateInRange(String startTime, String endTime, Date targetDate) {
 
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String targetDateStr = sdf.format(targetDate);
 
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate startDate = LocalDate.parse(startTime, formatter);
        LocalDate endDate = LocalDate.parse(endTime, formatter);
        LocalDate target = LocalDate.parse(targetDateStr, formatter);
 
        return target.isAfter(startDate) && target.isBefore(endDate);
    }
 
 
    public static boolean dateCompare( Date targetDate,String startTime) {
 
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String targetDateStr = sdf.format(targetDate);
 
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate startDate = LocalDate.parse(startTime, formatter);
 
        LocalDate target = LocalDate.parse(targetDateStr, formatter);
//        System.out.println(target.compareTo(startDate));
//        return target.isAfter(startDate);
        return target.compareTo(startDate)>=0;
    }
 
    /**
     * 获取当前年月日,yyyy/MM/dd格式
     * @return yyyy/MM/dd的字符串
     */
    public static String getCurrentDateCatalog() {
 
        Date date = new Date();
 
        // 使用SimpleDateFormat类来格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd/");
 
        return sdf.format(date);
    }
 
 
}