发布于 

DateUtil1

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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
package com.ioo.system.utils;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ioo.system.constant.CommonConstant;
import com.ioo.system.web.report_statistic.controller.vo.ReportDateVO;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.IsoFields;
import java.util.*;

/**
* @Project:iootools2
* @Package:com.ioo.system.utils
* @Author:xl
* @name:SysDateUtil
* @Date:2024/3/13 17:39
*/
public class SysDateUtil {

public static long targetDate(Date target) {

Calendar calendar = Calendar.getInstance();
calendar.setTime(target);

LocalDate today = LocalDate.now(); // 获取当前日期
LocalDate targetDate = LocalDate.of(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH));// 月份是从0开始的,所以需要+1

long daysBetween = ChronoUnit.DAYS.between(today.atStartOfDay(), targetDate.atStartOfDay());

return daysBetween;
}

//字符串日期排序
public static List<String> datesSort(List<String> dates) {
if (CollectionUtil.isEmpty(dates))
return dates;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dates.sort((d1, d2) -> {
try {
Date date1 = dateFormat.parse(d1);
Date date2 = dateFormat.parse(d2);
return date1.compareTo(date2);
} catch (Exception e) {
throw new RuntimeException(e);
}
});
return dates;
}

//找出2个日期中的每一天
public static List<String> findDates(String beginTime, String endTime)
throws ParseException {
List<String> allDate = new ArrayList();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

Date dBegin = sdf.parse(beginTime);
Date dEnd = sdf.parse(endTime);
allDate.add(sdf.format(dBegin));
Calendar calBegin = Calendar.getInstance();
// 使用给定的 Date 设置此 Calendar 的时间
calBegin.setTime(dBegin);
Calendar calEnd = Calendar.getInstance();
// 使用给定的 Date 设置此 Calendar 的时间
calEnd.setTime(dEnd);
// 测试此日期是否在指定日期之后
while (dEnd.after(calBegin.getTime())) {
// 根据日历的规则,为给定的日历字段添加或减去指定的时间量
calBegin.add(Calendar.DAY_OF_MONTH, 1);
allDate.add(sdf.format(calBegin.getTime()));
}
return allDate;
}

//找出2个日期中的每一天
public static List<String> findDates(Date beginTime, Date endTime)
throws ParseException {
return findDates(dateToString(beginTime, "yyyy-MM-dd"), dateToString(endTime, "yyyy-MM-dd"));
}

//计算2个日期相差的天数
public static int getDateSpanToDays(String startDateStr, String endDateStr) {
DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
int days = 0;
try {
Date star = dft.parse(startDateStr);//开始时间
Date endDay = dft.parse(endDateStr);//结束时间

if (!date1Less(star, endDay))
return days;

Long starTime = star.getTime();
Long endTime = endDay.getTime();
Long num = endTime - starTime;//时间戳相差的毫秒数
days = Math.toIntExact(num / 24 / 60 / 60 / 1000);
} catch (ParseException e) {
e.printStackTrace();
}
return days;
}

public static List<Date> getDateSpan(String startDateStr, String endDateStr) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date startDate = null;
Date endDate = null;
try {
startDate = dateFormat.parse(startDateStr);
endDate = dateFormat.parse(endDateStr);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(startDate);
List<Date> datesInRange = new ArrayList<>();
while (calendar.getTime().before(endDate) || calendar.getTime().equals(endDate)) {
Date currentDate = calendar.getTime();
datesInRange.add(currentDate);
calendar.add(Calendar.DATE, 1);
}

for (Date date : datesInRange) {
System.out.println(dateFormat.format(date));
}
return datesInRange;
}

public static ReportDateVO getWeeks(String dt) {

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate date2 = LocalDate.parse(dt, formatter);

int year = date2.getYear();
int month = date2.getMonthValue();
int day = date2.getDayOfMonth();

// 指定日期
LocalDate date = LocalDate.of(year, month, day);

// 获取当前年份的第几周
int weekOfYear = date.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR);

List<String> strings = weekToDayFormate(year, weekOfYear);

ReportDateVO build = ReportDateVO.builder().title(year + "年第" +
weekOfYear + "周").year(year).startDate(stringToDateToYMD(strings.get(0))).endDate(stringToDateToYMD(strings.get(1)))
.start(strings.get(0))
.end(strings.get(1))
.weekNum((weekOfYear)).build();

return build;
}

public static String replyEndTimeToAutoRemindSubmitTime(String replyEndTime, String autoRemindTime) {
if (ObjectUtil.isEmpty(autoRemindTime) || ObjectUtil.isEmpty(replyEndTime))
return "";
String[] split = autoRemindTime.split(",");
if (split.length != 2)
return "";


String[] split1 = split[1].split(":");
if (split1.length != 2)
return "";

int day = Integer.parseInt(split[0]);
int hours = Integer.parseInt(split1[0]);
int min = Integer.parseInt(split1[1]);

return stringDateSubtract(replyEndTime, day, hours, min);
}

public static String stringDateSubtract(String dateString, int day, int hours, int min) {
// String dateString = "2022-01-02 03:30:00";
dateString += ":00";
// 定义日期时间格式
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

// 将字符串日期时间转换为 LocalDateTime 对象
LocalDateTime date = LocalDateTime.parse(dateString, formatter);

// 执行减法运算
date = date.minusDays(day);
date = date.minusHours(hours);
date = date.minusMinutes(min);
return date.format(formatter);
}

public static String stringDateToString(String dateString) {
SimpleDateFormat originalFormat = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat targetFormat = new SimpleDateFormat("yyyy年MM月dd日");
String formattedDate = dateString;
try {
Date date = originalFormat.parse(dateString);
formattedDate = targetFormat.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
return formattedDate;
}

public static Long stringDateToTimestamp(String dateString) {
long timestamp = -999l;
try {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = formatter.parse(dateString);
timestamp = date.getTime();
} catch (Exception e) {
e.printStackTrace();
}
return timestamp;
}

public static String timeDistance(Date endDate, Date startTime) {
long nd = 1000 * 24 * 60 * 60;
long nh = 1000 * 60 * 60;
long nm = 1000 * 60;
// long ns = 1000;
// 获得两个时间的毫秒时间差异
long diff = endDate.getTime() - startTime.getTime();
// 计算差多少天
long day = diff / nd;
// 计算差多少小时
long hour = diff % nd / nh;
// 计算差多少分钟
long min = diff % nd % nh / nm;
// 计算差多少秒//输出结果
// long sec = diff % nd % nh % nm / ns;
return (day > 0L ? day + "天" : "") + (hour > 0L ? hour + "小时" : "") + min + "分钟";
}

public static ReportDateVO getMonths(String dt) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate date2 = LocalDate.parse(dt, formatter);
int year = date2.getYear();
int month = date2.getMonthValue();
String firstDay = getFirstDay(year, month, "yyyy-MM-dd");
String lastDay = getLastDay(year, month, "yyyy-MM-dd");
ReportDateVO build = ReportDateVO.builder().title(year + "年第" +
month + "月").year(year).startDate(stringToDateToYMD(firstDay, "yyyy-MM-dd")).endDate(stringToDateToYMD(lastDay, "yyyy-MM-dd"))
.start(firstDay)
.end(lastDay)
.month(month).build();

return build;
}

public static ReportDateVO getDays(String dt) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate date2 = LocalDate.parse(dt, formatter);
int year = date2.getYear();
int month = date2.getMonthValue();
int dayOfMonth = date2.getDayOfMonth();
String firstDay = getFirstDay(year, month, "yyyy-MM-dd");
String lastDay = getLastDay(year, month, "yyyy-MM-dd");
ReportDateVO build = ReportDateVO.builder().title(dt).year(year).day(dayOfMonth).startDate(stringToDateToYMD(firstDay, "yyyy-MM-dd")).endDate(stringToDateToYMD(lastDay, "yyyy-MM-dd"))
.start(firstDay)
.end(lastDay)
.month(month).build();

return build;
}

public static Boolean date1Less(String date1, String date2) {
Date d1 = stringToDateToYMD(date1, "yyyy-MM-dd");
Date d2 = stringToDateToYMD(date2, "yyyy-MM-dd");
return (d1.compareTo(d2)) <= 0;
}

public static Boolean date1LessByHM(String date1, String date2) {
Date d1 = stringToDateToYMD(date1, "yyyy-MM-dd HH:mm");
Date d2 = stringToDateToYMD(date2, "yyyy-MM-dd HH:mm");
return (d1.compareTo(d2)) <= 0;
}

public static Boolean date1Less(Date date1, Date date2) {
return (date1.compareTo(date2)) <= 0;
}

public static Date stringToDateToYMD(String date) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date parse = new Date();
try {
parse = format.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return parse;
}

public static Date stringToDateToYMDHMS(String date) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date parse = new Date();
try {
parse = format.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return parse;
}

public static Date stringToDateToYMD(String date, String ft) {
SimpleDateFormat format = new SimpleDateFormat(ft);
Date parse = new Date();
try {
parse = format.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return parse;
}

//判断日期是否是今天
public static Boolean isToday(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);

Calendar today = Calendar.getInstance();
return today.get(Calendar.YEAR) == calendar.get(Calendar.YEAR) &&
today.get(Calendar.MONTH) == calendar.get(Calendar.MONTH) &&
today.get(Calendar.DAY_OF_MONTH) == calendar.get(Calendar.DAY_OF_MONTH);
}

public static Date stringToDateHMS(String date) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd h:m");
Date parse = new Date();
try {
parse = format.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return parse;
}

public static String dateToString(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(date);
}

public static String dateToString(Date date, String ft) {
SimpleDateFormat sdf = new SimpleDateFormat(ft);
return sdf.format(date);
}

//修改日期的时间部分
public static Date setDt(Date time, int h, int min) {
Calendar cal = Calendar.getInstance();
cal.setTime(time);
cal.set(Calendar.HOUR_OF_DAY, h); //时
cal.set(Calendar.MINUTE, min); //分
cal.set(Calendar.SECOND, 0); //秒
cal.set(Calendar.MILLISECOND, 0); //毫秒
return cal.getTime();
}

// 获取指定年的开始天数和结束天数
public static List<Date> getFirstDayAndLastDayOfTheSpecifiedYear(int year) {
Calendar calendar = Calendar.getInstance();
// 设置某年的开始时间
calendar.set(year, Calendar.JANUARY, 1, 0, 0, 0);
calendar.set(Calendar.MILLISECOND, 0);
Date startTime = calendar.getTime();

// 设置某年的结束时间
calendar.set(year, Calendar.DECEMBER, 31, 23, 59, 59);
calendar.set(Calendar.MILLISECOND, 999);
Date endTime = calendar.getTime();
// 设置返回信息,返回样式根据需求自行格式化
List<Date> years = new ArrayList<>();
years.add(setDt(startTime, 0, 0));
years.add(setDt(endTime, 23, 59));
return years;
}

// 获取指定年指定月的开始天数和结束天数
public static List<Date> getFirstDayAndLastDayOfTheSpecifiedMonth(int year, int month) {
// 获取当前分区的日历信息(这里可以使用参数指定时区)
Calendar calendar = Calendar.getInstance();
// 设置年
calendar.set(Calendar.YEAR, year);
// 设置月,月份从0开始
calendar.set(Calendar.MONTH, month - 1);
// 设置为指定月的第一天
calendar.set(Calendar.DAY_OF_MONTH, 1);
// 获取指定月第一天的时间
Date start = calendar.getTime();
// 设置日历天数为当前月实际天数的最大值,即指定月份的最后一天
calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
// 获取最后一天的时间
Date end = calendar.getTime();
// 设置返回信息,返回样式根据需求自行格式化
List<Date> months = new ArrayList<>();
months.add(setDt(start, 0, 0));
months.add(setDt(end, 23, 59));
return months;
}


//获取当前月份的开始和结束时间
public static List<Date> getCurMonth() {
List<Date> month = new ArrayList<>();
// 获取当前日期
Date currentDate = new Date();
// 创建Calendar实例
Calendar calendar = Calendar.getInstance();
// 设置日期为当前日期
calendar.setTime(currentDate);
// 将日期设置为该月的第一天
calendar.set(Calendar.DAY_OF_MONTH, 1);
// 获取本月的开始时间
Date startTime = calendar.getTime();
// 将日期设置为该月的最后一天
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
// 获取本月的结束时间
Date endTime = calendar.getTime();
month.add(setDt(startTime, 0, 0));
month.add(setDt(endTime, 23, 59));

return month;
}

//获取当前时间的年月日
public static String getCurrentDate() {
Date currentDate = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = dateFormat.format(currentDate);
return formattedDate;
}

//获取当前时间的年月日时分秒
public static String getCurrentDateHMS() {
Date currentDate = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = dateFormat.format(currentDate);
return formattedDate;
}

//获取N天前的年月日
public static String getDateSpan() {
LocalDate today = LocalDate.now(); // 获取当前日期
LocalDate oneHundredEightyDaysAgo = today.minusDays(CommonConstant.WEEK_DATE_SPAN); // 当前日期减去365天
// 格式化日期为年月日
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String formattedDate = oneHundredEightyDaysAgo.format(formatter);
return formattedDate;
}

// 计算两个时间中所有的月份
public static List<String> getMonths(String startDate, String endDate) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date parse = sdf.parse(startDate);
Date parse2 = sdf.parse(endDate);
List<String> dateList = new ArrayList<>();
Calendar startC = Calendar.getInstance();
startC.setTime(parse);
//转为周一
int year = startC.get(Calendar.YEAR);
int month = startC.get(Calendar.MONTH);
startC.set(year, month, 1, 0, 0, 0);
Calendar endC = Calendar.getInstance();
endC.setTime(parse2);
int weekYear2 = endC.get(Calendar.YEAR);
int weekOfYear2 = endC.get(Calendar.WEEK_OF_YEAR);
endC.setWeekDate(weekYear2, weekOfYear2, Calendar.SUNDAY);
while (true) {
int tempMonth = startC.get(Calendar.MONTH);
String date = startC.getWeekYear() + "-" + ((tempMonth + 1) <= 9 ? "0" + (tempMonth + 1) : tempMonth + 1);
dateList.add(date);
//下一个月<结束日期
startC.set(Calendar.MONTH, tempMonth + 1);
if (startC.getTimeInMillis() >= endC.getTimeInMillis()) {
break;
}
}
return dateList;
}

private static Long dateScaleplate(Calendar calendar) {
int year = calendar.get(Calendar.YEAR);
// 获取月份(月份是从0开始的,因此需要+1)
// int month = calendar.get(Calendar.MONTH) + 1;
// int day = calendar.get(Calendar.DAY_OF_MONTH);
int weekNum = calendar.get(Calendar.WEEK_OF_YEAR);
String dt = year + "" + weekNum;
return Long.parseLong(dt);
}

// 计算两个时间内 所有的周数
public static List<ReportDateVO> getWeeks(String startDate, String endDate) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date parse = sdf.parse(startDate);
Date parse2 = sdf.parse(endDate);
List<ReportDateVO> dateList = new ArrayList<>();
Calendar startC = Calendar.getInstance();
startC.setFirstDayOfWeek(Calendar.MONDAY);
startC.setTime(parse);
Long startCurrentWeek = dateScaleplate(startC);
//转为周一
int weekYear = startC.get(Calendar.YEAR);
int weekOfYear = startC.get(Calendar.WEEK_OF_YEAR);
startC.setWeekDate(weekYear, weekOfYear, Calendar.MONDAY);
Calendar endC = Calendar.getInstance();
endC.setFirstDayOfWeek(Calendar.MONDAY);
endC.setTime(parse2);
Long endWeek = dateScaleplate(endC);
int weekYear2 = endC.get(Calendar.YEAR);
int weekOfYear2 = endC.get(Calendar.WEEK_OF_YEAR);
endC.setWeekDate(weekYear2, weekOfYear2, Calendar.SUNDAY);
while (true) {
int weekNum = startC.get(Calendar.WEEK_OF_YEAR);
ReportDateVO build = ReportDateVO.builder().title(startC.getWeekYear() + "年第" +
(weekNum > 9 ? weekNum : "0" + weekNum) + "周").year(startC.getWeekYear())
.weekNum((weekNum > 9 ? weekNum : Integer.valueOf("0" + weekNum))).build();
dateList.add(build);
// if (c1.getTimeInMillis() >= c2.getTimeInMillis()) {
// System.out.println("22-c1:"+cdt1+"-c2:"+cdt2);
// break;
// }
if (startCurrentWeek >= endWeek) {
break;
}
//增加7天
startC.setTimeInMillis(startC.getTimeInMillis() + 1000 * 60 * 60 * 24 * 7);
startCurrentWeek = dateScaleplate(startC);
}
return dateList;
}

/**
* 根据年月获取月初第一天日期
*
* @param year
* @param month
* @return
*/
public static String getFirstDay(int year, int month, String format) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month - 1);
calendar.set(Calendar.DAY_OF_MONTH, 1);

calendar.set(Calendar.HOUR_OF_DAY, 0); //时
calendar.set(Calendar.MINUTE, 0); //分
calendar.set(Calendar.SECOND, 0); //秒
calendar.set(Calendar.MILLISECOND, 0); //毫秒

// TimeZone timeZone = TimeZone.getTimeZone("UTC");
// calendar.setTimeZone(timeZone);

Date startDate = calendar.getTime();

SimpleDateFormat sdf = new SimpleDateFormat(format);
String formattedStartDate = sdf.format(startDate);

return formattedStartDate;
}


/**
* 根据年月获取月末最后一天日期
*
* @param year
* @param month
* @return
*/
public static String getLastDay(int year, int month, String format) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);

calendar.set(Calendar.DAY_OF_MONTH, 1);

calendar.add(Calendar.DAY_OF_MONTH, -1);

calendar.set(Calendar.HOUR_OF_DAY, 23); //时
calendar.set(Calendar.MINUTE, 59); //分
calendar.set(Calendar.SECOND, 59); //秒
calendar.set(Calendar.MILLISECOND, 0); //毫秒

// TimeZone timeZone = TimeZone.getTimeZone("UTC");
// calendar.setTimeZone(timeZone);


Date endDate = calendar.getTime();

SimpleDateFormat sdf = new SimpleDateFormat(format);
String formattedEndDate = sdf.format(endDate);

return formattedEndDate;
}

// 根据year年的第week周,查询本周的起止时间
public static List<String> weekToDayFormate(int year, int week) {
Calendar calendar = Calendar.getInstance();
// ①.设置该年份的开始日期:第一个月的第一天
calendar.set(year, 0, 1);
List<String> dateList = new ArrayList<>();
// ②.计算出第一周还剩几天:+1是因为1号是1天
int dayOfWeek = 7 - calendar.get(Calendar.DAY_OF_WEEK) + 1;
// ③.周数减去第一周再减去要得到的周
week = week - 2;
// ④.计算起止日期
calendar.add(Calendar.DAY_OF_YEAR, week * 7 + dayOfWeek);
calendar.add(Calendar.DAY_OF_YEAR, 1);

TimeZone timeZone = TimeZone.getTimeZone("GMT+8");
calendar.setTimeZone(timeZone);

dateList.add(new SimpleDateFormat("yyyy-MM-dd 00:00:00").format(calendar.getTime()));
// System.out.println("开始日期:" + new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()));
calendar.add(Calendar.DAY_OF_YEAR, 6);

// calendar.set(Calendar.HOUR_OF_DAY, 23); //时
// calendar.set(Calendar.MINUTE, 59); //分
// calendar.set(Calendar.SECOND, 59); //秒
// calendar.set(Calendar.MILLISECOND, 0); //毫秒

dateList.add(new SimpleDateFormat("yyyy-MM-dd 23:59:59").format(calendar.getTime()));
// System.out.println("结束日期:" + new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()));
return dateList;
}

public static List<String> weekToDayFormate2(int year, int week) {
List<String> dateList = new ArrayList<>();
Calendar calendar = Calendar.getInstance();
calendar.set(year, 0, 1);
int dayOfWeek = 7 - calendar.get(Calendar.DAY_OF_WEEK) + 1;//算出第一周还剩几天 +1是因为1号是1天
week = week - 2;//周数减去第一周再减去要得到的周
calendar.add(Calendar.DAY_OF_YEAR, week * 7 + dayOfWeek);
dateList.add(new SimpleDateFormat("yyyy-MM-dd 00:00:00").format(calendar.getTime()));
calendar.add(Calendar.DAY_OF_YEAR, 6);
dateList.add(new SimpleDateFormat("yyyy-MM-dd 23:59:59").format(calendar.getTime()));
return dateList;
}

// 计算两个时间内所有日期
public static List<String> getDays(String date1, String date2) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Long startTime = sdf.parse(date1).getTime();
Long endTime = sdf.parse(date2).getTime();
List<String> dateList = new ArrayList<String>();
Long oneDay = 1000 * 60 * 60 * 24l;
Long time = startTime;
while (time <= endTime) {
Date d = new Date(time);
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = df.format(d);
dateList.add(date);
time += oneDay;
}
return dateList;
}
}