chenjiahe
2024-06-14 1898bc40ca08ca02be9ac74a9c353dea3d17f120
提交 | 用户 | age
5c5945 1 package com.hx.util;
E 2
9c8552 3 import java.math.BigDecimal;
C 4 import java.math.RoundingMode;
5c5945 5 import java.text.ParseException;
E 6 import java.text.SimpleDateFormat;
7 import java.util.Calendar;
8 import java.util.Date;
9 import java.util.Locale;
10
11 public class DateUtil {
12
13     private static SimpleDateFormat Format_1 = new SimpleDateFormat("yyyyMMdd");
14     private static SimpleDateFormat Format_2 = new SimpleDateFormat("yyyyMMddHHmmss");
15     private static SimpleDateFormat Format_3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
16     private static SimpleDateFormat Format_4 = new SimpleDateFormat("yyyy-MM-dd");
17     private static SimpleDateFormat Format_5 = new SimpleDateFormat("HH:mm:ss");
18     private static SimpleDateFormat Format_6 = new SimpleDateFormat("yyyyMM");
19     private static SimpleDateFormat Format_7 = new SimpleDateFormat("MM月dd日 HH:mm");
20     private static SimpleDateFormat Format_8 = new SimpleDateFormat("yyyy年MM月");
21     private static SimpleDateFormat Format_9 = new SimpleDateFormat("MM-dd");
22     private static SimpleDateFormat Format_10 = new SimpleDateFormat("yyyy年MM月dd日");
23     private static SimpleDateFormat Format_11 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
24     private static SimpleDateFormat Format_12 = new SimpleDateFormat("yyyyMMddHHmm");
25     private static SimpleDateFormat Format_13 = new SimpleDateFormat("yyyy/MM/dd");
26     private static SimpleDateFormat Format_14 = new SimpleDateFormat("yyyy-MM");
27     private static SimpleDateFormat Format_15 = new SimpleDateFormat("yyyyMMddHHmmssSSS");
28     private static SimpleDateFormat Format_16 = new SimpleDateFormat("yyyy/MM/dd HH:mm");
29     private static SimpleDateFormat Format_17 = new SimpleDateFormat("HH:mm");
30
894d12 31     /**时间格式转化iso8601
C 32      * @param date 时间
33      * @return 返回的时间格式字符串
34      */
35     public static String dateFormatISO8601(Date date) {
36         if(!SimpleTool.checkNotNull(date)){
37             return "";
38         }
39         SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");//设置日期格式
40         return df.format(date);
41     }
42
5c5945 43     /**时间格式转化
E 44      * @param date 时间
45      * @param format 时间格式
46      * @return 返回的时间格式字符串
47      */
48     public static String dateFormat(Date date, String format) {
49         if(!SimpleTool.checkNotNull(date)){
50             return "";
51         }
52         SimpleDateFormat df = new SimpleDateFormat(format);//设置日期格式
53         return df.format(date);
54     }
55
f2a234 56     /**时间戳转时间
C 57      * @param timestamp 时间戳
58      * @param format 时间格式
59      * @return 返回的时间格式字符串
60      */
61     public static Date timestampToDate(long timestamp, String format) {
62         SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
63         String sd = sdf.format(new Date(timestamp));      // 时间戳转换成时间
64         return DateUtil.parseString(sd,"yyyy-MM-dd HH:mm:ss");
65     }
66
5c5945 67     /**
E 68      * 转换成yyyyMMdd格式的日期字符串
69      *
70      * @param date
71      * @return
72      */
73     public static String formatDate(Date date) {
74         return Format_1.format(date);
75     }
76
77     /**
78      * 转换成yyyyMMddHHmmss格式的日期字符串
79      *
80      * @param date
81      * @return
82      */
83     public static String formatDate_1(Date date) {
84         return Format_2.format(date);
85     }
86
87     /**
88      * 转换成yyyy-MM-dd HH:mm:ss格式的日期字符串
89      *
90      * @param date
91      * @return
92      */
93     public static String formatDate_2(Date date) {
94         return date == null ? null : Format_3.format(date);
95     }
96
97     /**
98      * 转换成yyyy-MM-dd格式的日期字符串
99      *
100      * @param date
101      * @return
102      */
103     public static String formatDate_3(Date date) {
104         return date != null ? Format_4.format(date) : null;
105     }
106
107     /**
108      * 转换成HH:mm:ss格式的日期字符串
109      *
110      * @param date
111      * @return
112      */
113     public static String formatDate_4(Date date) {
114         return Format_5.format(date);
115     }
116
117     /**
118      * 转换成yyyyMM格式的日期字符串
119      *
120      * @param date
121      * @return
122      */
123     public static String formatDate_5(Date date) {
124         return Format_6.format(date);
125     }
126
127     /**
128      * 转换成mm月dd日 HH:mm格式的日期字符串
129      *
130      * @param date
131      * @return
132      */
133     public static String formatDate_6(Date date) {
134         return Format_7.format(date);
135     }
136
137     /**
138      * 转换成yyyy年mm月格式的日期字符串
139      *
140      * @param date
141      * @return
142      */
143     public static String formatDate_7(Date date) {
144         return Format_8.format(date);
145     }
146
147     /**
148      * 转换成mm-dd格式的日期字符串
149      *
150      * @param date
151      * @return
152      */
153     public static String formatDate_8(Date date) {
154         return Format_9.format(date);
155     }
156
157     /**
158      * 转换成yyyy年MM月dd日格式的日期字符串
159      *
160      * @param date
161      * @return
162      */
163     public static String formatDate_9(Date date) {
164         return Format_10.format(date);
165     }
166
167     /**
168      * 转换成yyyyMMddHHmm格式的日期字符串
169      *
170      * @param date
171      * @return
172      */
173     public static String formatDate_11(Date date) {
174         return Format_12.format(date);
175     }
176
177     /**
178      * 转换成yyyy年MM月dd日 HH:mm:ss格式的日期字符串
179      *
180      * @param date
181      * @return
182      */
183     public static String formatDate_10(Date date) {
184         return Format_11.format(date);
185     }
186
187     /**
188      * 转换成yyyy/MM/dd 格式的日期字符串
189      *
190      * @param date
191      * @return
192      */
193     public static String formatDate_13(Date date) {
194         return Format_13.format(date);
195     }
196
197     /**
198      * 转换成yyyy-MM 格式的日期字符串
199      *
200      * @param date
201      * @return
202      */
203     public static String formatDate_14(Date date) {
204         return Format_14.format(date);
205     }
206
1ffbaa 207
W 208     /**
209      * 当前时间之前的时间与当前时间相差多少秒
210      * @param startDate 当前时间之前的时间
211      * @return
212      */
213     public static int calLastedTime(Date startDate) {
214         long nowDate = new Date().getTime();
215         long startDateTime = startDate.getTime();
216         int diffSeconds = (int) ((nowDate - startDateTime) / 1000);
217         return diffSeconds;
218     }
219
220
5c5945 221     /**
E 222      * 转换成yyyyMMddHHmmssSSS格式的日期字符串
223      *
224      * @param date
225      * @return
226      */
227     public static String formatDate_15(Date date) {
228         return Format_15.format(date);
229     }
230
231     /**
232      * 转换成yyyy/MM/dd HH:mm格式的日期字符串
233      *
234      * @param date
235      * @return
236      */
237     public static String formatDate_16(Date date) {
238         return Format_16.format(date);
239     }
240
241     /**
242      * 转换成HH:mm格式的日期字符串
243      *
244      * @param date
245      * @return
246      */
247     public static String formatDate_17(Date date) {
248         return Format_17.format(date);
249     }
250
251     /**
252      * 转换成字符串yyyyMMddHHmmss成日�?
253      *
254      * @param str
255      * @return
256      * @throws Exception
257      */
190d78 258     public static Date parseString(String str) {
E 259         try {
260             return Format_2.parse(str);
261         }catch (Exception e)
262         {
263             return null;
264         }
5c5945 265     }
E 266
267     /**
268      * 转换成字符串到指定的日期
269      *
270      * @param str
271      * @param format
272      * @return
273      * @throws Exception
274      */
190d78 275     public static Date parseString(String str, String format) {
5c5945 276         SimpleDateFormat sdf = new SimpleDateFormat(format);
190d78 277         try{
E 278             return sdf.parse(str);
279         }catch (Exception e)
280         {
281             return null;
282         }
5c5945 283     }
E 284
cd868c 285     /**字符串转成时间yyyy-MM-dd HH:mm:ss*/
E 286     public static Date parseString_1(String str)
287     {
288         try{
289             return  Format_3.parse(str);
290         }catch (Exception e)
291         {
292             return null;
293         }
294     }
295
296     /**字符串转成时间yyyy-MM-dd*/
297     public static Date parseString_2(String str)
298     {
299         try{
300             return Format_4.parse(str);
301         }catch (Exception e)
302         {
303             return null;
304         }
305     }
306
1daac0 307     /**时间上秒叠加
C 308      * @Author: ChenJiaHe
309      * @param dateTime 时间
310      * @param second 秒
311      * @return
312      */
313     public static Date addSecond(Date dateTime,int second){
314         Calendar c = Calendar.getInstance();
315         c.setTime(dateTime);
316         c.add(Calendar.SECOND, second);
317         return c.getTime();
318     }
cd868c 319
5c5945 320     /**时间上分钟叠加
E 321      * @Author: ChenJiaHe
322      * @param dateTime 时间
323      * @param min 分钟
324      * @return
325      */
326     public static Date addMin(Date dateTime,int min){
327         Calendar c = Calendar.getInstance();
328         c.setTime(dateTime);
329         c.add(Calendar.MINUTE, min);
330         return c.getTime();
331     }
332
333     /**时间上小时叠加
334      * @Author: ChenJiaHe
335      * @param dateTime 时间
336      * @param hour 小时
337      * @return
338      */
339     public static Date addhour(Date dateTime,int hour){
340         Calendar c = Calendar.getInstance();
341         c.setTime(dateTime);
342         c.add(Calendar.HOUR, hour);
343         return c.getTime();
344     }
345
346     /**时间上天数叠加
347      * @Author: ChenJiaHe
348      * @param dateTime 时间
349      * @param dayNum 天数
350      * @return
351      */
352     public static Date addDay(Date dateTime,int dayNum){
353         Calendar c = Calendar.getInstance();
354         c.setTime(dateTime);
355         c.add(Calendar.DATE, dayNum);
356         return c.getTime();
357     }
358
76b630 359     /**时间上月数叠加
C 360      * @Author: ChenJiaHe
361      * @param dateTime 时间
362      * @param dayNum 天数
363      * @return
364      */
365     public static Date addMonth(Date dateTime,int dayNum){
366         Calendar c = Calendar.getInstance();
367         c.setTime(dateTime);
368         c.add(Calendar.MONTH, dayNum);
369         return c.getTime();
370     }
371
f06126 372     /**时间上年数叠加
C 373      * @Author: ChenJiaHe
374      * @param dateTime 时间
375      * @param dayNum 天数
376      * @return
377      */
378     public static Date addYear(Date dateTime,int dayNum){
379         Calendar c = Calendar.getInstance();
380         c.setTime(dateTime);
381         c.add(Calendar.YEAR, dayNum);
382         return c.getTime();
383     }
384
5c5945 385     /**
E 386      * 转换成字符串到指定的日期
387      *
388      * @param date
389      * @param format
390      * @return
391      * @throws Exception
392      */
393     public static String formatDate(Date date, String format) {
394         if (date == null) {
395             return null;
396         }
397
398         SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.CHINA);
399         return sdf.format(date);
400     }
401
402     /**日期转星期
403      * @param dateTime 字符串时间
404      * @param format 字符串时间格式
405      * @param returnType 返回类型 0星期几1周几
406      * @return 数据
407      */
408     public static String dateToWeek(String dateTime,String format,Integer returnType) throws ParseException {
409         SimpleDateFormat f = new SimpleDateFormat(format);
410         String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
411         if(SimpleTool.checkNotNull(returnType)&&returnType==1){
412             weekDays  = new String[]{"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
413         }
414         Calendar cal = Calendar.getInstance(); // 获得一个日历
415         Date datet = null;
416         datet = f.parse(dateTime);
417         cal.setTime(datet);
418
419         int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
420         if (w < 0)
421             w = 0;
422         return weekDays[w];
423     }
424
425     /**日期转星期
426      * @param dateTime 时间
427      * @param returnType 返回类型 0星期几1周几
428      * @return 数据
429      */
430     public static String dateToWeek(Date dateTime,Integer returnType) {
431         String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
432         if(SimpleTool.checkNotNull(returnType)&&returnType==1){
433             weekDays  = new String[]{"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
434         }
435         Calendar cal = Calendar.getInstance(); // 获得一个日历
436         cal.setTime(dateTime);
437
438         int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
439         if (w < 0)
440             w = 0;
441         return weekDays[w];
442     }
443
db7fc9 444     /**
336175 445      * 获取两个时间相差分钟数
A 446      * @param startTime 开始时间
447      * @param endTime 结束时间
448      * @return 分钟
449      */
450     public static long differMinute(Date startTime, Date endTime) {
451         long NTime = startTime.getTime();
452         long OTime = endTime.getTime();
453         return (NTime - OTime) / 1000 / 60;
454     }
455
456     /**
9c8552 457      * 获取两个时间相差分钟数
C 458      * @param startTime 开始时间
459      * @param endTime 结束时间
460      * @param remainder 余数进1
461      * @return 分钟
462      */
463     public static int differMinute(Date startTime, Date endTime,boolean remainder) {
464         BigDecimal sTime = new BigDecimal(startTime.getTime());
465         BigDecimal eTime = new BigDecimal(endTime.getTime());
466         eTime = eTime.subtract(sTime).setScale(0,RoundingMode.HALF_UP);
467         if(remainder){
468             eTime = eTime.divide(BigDecimal.valueOf(60000.0)).setScale(0, RoundingMode.UP);
469         }else{
470             eTime = eTime.divide(BigDecimal.valueOf(60000.0)).setScale(0, RoundingMode.DOWN);
471         }
472         return eTime.intValue();
473     }
474
475     /**
db7fc9 476      * endTime比startTime多的天数
C 477      * @param startTime 最小时间
478      * @param endTime 最大时间
578f7b 479      * @return 返回
db7fc9 480      */
C 481     public static Integer differDay(Date startTime,Date endTime){
482         Calendar cal1 = Calendar.getInstance();
483         cal1.setTime(startTime);
484
485         Calendar cal2 = Calendar.getInstance();
486         cal2.setTime(endTime);
487         int day1= cal1.get(Calendar.DAY_OF_YEAR);
488         int day2 = cal2.get(Calendar.DAY_OF_YEAR);
489
490         int year1 = cal1.get(Calendar.YEAR);
491         int year2 = cal2.get(Calendar.YEAR);
492         if(year1 != year2) { //同一年
493             int timeDistance = 0 ;
494             for(int i = year1 ; i < year2 ; i ++) {
495                 if(i%4==0 && i%100!=0 || i%400==0) {//闰年
496                     timeDistance += 366;
497                 }
498                 else {//不是闰年
499                     timeDistance += 365;
500                 }
501             }
502
503             return timeDistance + (day2-day1) ;
504         }
505         else { //不同年
506             System.out.println("判断day2 - day1 : " + (day2-day1));
507             return day2-day1;
508         }
509     }
510
5c5945 511     /**判断两个时间是不是同一天*/
E 512     public static boolean timeEqual(Date startTime,Date endTime){
513        if(startTime == null || endTime==null){
514            return false;
515        }
516        boolean status = false;
517        if(formatDate(startTime,"yyyyMMdd").equals(formatDate(endTime,"yyyyMMdd"))){
6f2306 518            status = true;
5c5945 519        }
E 520        return status;
521     }
522
63b0b0 523     /**把秒转换成X天X时X分X秒*/
578f7b 524     public static String getChineseStr(Integer second) {
63b0b0 525         int day = 24 * 60 * 60;
E 526         int hour = 60 * 60;
527         int min = 60;
528
529         int dayNum = second / day;
530         int hourNum = second % day / hour;
531         int minNum = second % day % hour / min;
532         second = second % day % hour % min;
533
534         String str = dayNum > 0 ? dayNum + "天" : "";
535         str += hourNum > 0 ? hourNum + "时" : "";
536         str += minNum > 0 ? minNum + "分" : "";
0ff9b8 537         str += second + "秒";
63b0b0 538
E 539         return str;
540     }
541
387efa 542     /**
E 543      * 针对str格式的时间做转换 格式为"xx:xx"
544      * @param time  传入的时间
545      * @return  返回分钟如果10:25,则返回625
546      */
547     public static int getMinuteNum(String time){
548
549         if(!StringUtils.isEmpty(time))
550         {
551             String[] arr = time.split(":");
552             if(arr != null && arr.length == 2)
553             {
554                 return Integer.parseInt(arr[0]) * 60 + Integer.parseInt(arr[1]);
555             }
556         }
557
558         return 0;
2e1c15 559     }
387efa 560
2e1c15 561     /**
C 562      * 获取当前月的开始时间
563      * @param time 时间
564      * @return 返回时间 格式yyyy-MM-dd 00:00:00
565      */
566     public static Date getMonthStart(Date time) {
567         Calendar calendar = Calendar.getInstance();
568         calendar.setTime(time);
569         calendar.set(Calendar.DAY_OF_MONTH, 1);
570         calendar.set(Calendar.HOUR_OF_DAY, 0);
571         calendar.set(Calendar.MINUTE, 0);
572         calendar.set(Calendar.SECOND, 0);
f610e6 573         calendar.set(Calendar.MILLISECOND,0);
2e1c15 574         return calendar.getTime();
387efa 575     }
E 576
ba61c9 577     /**
F 578      * 获取当前月的开始时间
579      *
580      * @param num 0拿取当月,正代表后,负代表前,值为几个(月)
581      * @return 返回时间 格式yyyy-MM-dd 00:00:00
582      */
583     public static String getMonthStart(Integer num) {
584         Calendar calendar = Calendar.getInstance();
585         calendar.add(Calendar.MONTH, num);
586         calendar.set(Calendar.DAY_OF_MONTH, 1);
587         calendar.set(Calendar.HOUR_OF_DAY, 0);
588         calendar.set(Calendar.MINUTE, 0);
589         calendar.set(Calendar.SECOND, 0);
590         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
591         return sdf.format(calendar.getTime());
2e1c15 592     }
C 593
594     /**
595      * 获取当前月的结束时间
ca698f 596      * @param date 点前时间
c2bb89 597      * @return 返回时间 格式yyyy-MM-dd 23:59:59999
2e1c15 598      */
C 599     public static Date getMonthEnd(Date date) {
600         Calendar calendar = Calendar.getInstance();
601         calendar.setTime(date);
602         calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
603         calendar.set(Calendar.HOUR_OF_DAY, 23);
604         calendar.set(Calendar.MINUTE, 59);
605         calendar.set(Calendar.SECOND, 59);
f610e6 606         calendar.set(Calendar.MILLISECOND,999);
2e1c15 607         return calendar.getTime();
ba61c9 608     }
F 609
610     /**
85234c 611      * 获取当前月的结束时间,没有毫秒
C 612      * @param date 点前时间
613      * @return 返回时间 格式yyyy-MM-dd 23:59:59
614      */
615     public static Date getMonthEndNoMillisecond(Date date) {
616         Calendar calendar = Calendar.getInstance();
617         calendar.setTime(date);
618         calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
619         calendar.set(Calendar.HOUR_OF_DAY, 23);
620         calendar.set(Calendar.MINUTE, 59);
621         calendar.set(Calendar.SECOND, 59);
622         return calendar.getTime();
623     }
624
625     /**
ba61c9 626      * 获取当前月的结束时间
F 627      * @param num 0拿取当月,正代表后,负代表前,值为几个(月)
628      * @return 返回时间 格式yyyy-MM-dd 23:59:59
629      */
630     public static String getMonthEnd(Integer num) {
631         Calendar calendar = Calendar.getInstance();
632         calendar.add(Calendar.MONTH, num);
633         calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
634         calendar.set(Calendar.HOUR_OF_DAY, 23);
635         calendar.set(Calendar.MINUTE, 59);
636         calendar.set(Calendar.SECOND, 59);
637         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
638         return sdf.format(calendar.getTime());
639     }
640
5bfcd3 641     /**
C 642      * 获取当前年的开始时间
643      * @param time 时间
644      * @return 返回时间 格式yyyy-MM-dd 00:00:00
645      */
646     public static Date getYearStart(Date time) {
647         Calendar calendar = Calendar.getInstance();
648         calendar.setTime(time);
a7fa7d 649         calendar.set(Calendar.MONTH, 0);
5bfcd3 650         calendar.set(Calendar.DAY_OF_MONTH, 1);
C 651         calendar.set(Calendar.HOUR_OF_DAY, 0);
652         calendar.set(Calendar.MINUTE, 0);
653         calendar.set(Calendar.SECOND, 0);
f610e6 654         calendar.set(Calendar.MILLISECOND,0);
5bfcd3 655         return calendar.getTime();
C 656     }
657
ba61c9 658     /**
ca698f 659      * 获取当前年的结束时间
C 660      * @param time 时间
c2bb89 661      * @return 返回时间 格式yyyy-MM-dd 23:59:59999
ca698f 662      */
C 663     public static Date getYearEnd(Date time) {
664         Calendar calendar = Calendar.getInstance();
665         calendar.setTime(time);
a7fa7d 666         calendar.set(Calendar.MONTH, 11);
ca698f 667         calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DATE));
C 668         calendar.set(Calendar.HOUR_OF_DAY, 23);
669         calendar.set(Calendar.MINUTE, 59);
670         calendar.set(Calendar.SECOND, 59);
f610e6 671         calendar.set(Calendar.MILLISECOND,999);
ca698f 672         return calendar.getTime();
C 673     }
674
85234c 675     /**
C 676      * 获取当前年的结束时间,没有毫秒
677      * @param time 时间
678      * @return 返回时间 格式yyyy-MM-dd 23:59:59
679      */
680     public static Date getYearEndNoMillisecond(Date time) {
681         Calendar calendar = Calendar.getInstance();
682         calendar.setTime(time);
683         calendar.set(Calendar.MONTH, 11);
684         calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DATE));
685         calendar.set(Calendar.HOUR_OF_DAY, 23);
686         calendar.set(Calendar.MINUTE, 59);
687         calendar.set(Calendar.SECOND, 59);
688         return calendar.getTime();
689     }
690
ca698f 691
C 692     /**这天的开始时间
ba61c9 693      * 日期2000-01-01变2000-01-01 00:00:00
F 694      */
695     public static String dayToStart(Date date) {
696         Calendar calendar = Calendar.getInstance();
697         calendar.setTime(date);
05b175 698         calendar.set(Calendar.HOUR_OF_DAY, 0);
ba61c9 699         calendar.set(Calendar.MINUTE, 0);
F 700         calendar.set(Calendar.SECOND, 0);
701         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
702         return sdf.format(calendar.getTime());
703     }
704
ca698f 705     /**这天的最后时间
ba61c9 706      * 日期2000-01-01变2000-01-01 23:59:59
F 707      */
708     public static String dayToEnd(Date date) {
709         Calendar calendar = Calendar.getInstance();
710         calendar.setTime(date);
05b175 711         calendar.set(Calendar.HOUR_OF_DAY, 23);
ba61c9 712         calendar.set(Calendar.MINUTE, 59);
F 713         calendar.set(Calendar.SECOND, 59);
714         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
715         return sdf.format(calendar.getTime());
5bfcd3 716     }
C 717
5acd00 718     /**这天的开始时间
C 719      * 日期2000-01-01变2000-01-01 00:00:00
720      */
721     public static Date dayToStartDate(Date date) {
722         Calendar calendar = Calendar.getInstance();
723         calendar.setTime(date);
724         calendar.set(Calendar.HOUR_OF_DAY, 0);
725         calendar.set(Calendar.MINUTE, 0);
726         calendar.set(Calendar.SECOND, 0);
f610e6 727         calendar.set(Calendar.MILLISECOND, 0);
5acd00 728         return calendar.getTime();
C 729     }
730
ca698f 731     /**这天的最后时间
85234c 732      * 日期2000-01-01变2000-01-01 23:59:59999
5bfcd3 733      */
C 734     public static Date dayToEndDate(Date date) {
735         Calendar calendar = Calendar.getInstance();
736         calendar.setTime(date);
05b175 737         calendar.set(Calendar.HOUR_OF_DAY, 23);
5bfcd3 738         calendar.set(Calendar.MINUTE, 59);
C 739         calendar.set(Calendar.SECOND, 59);
f610e6 740         calendar.set(Calendar.MILLISECOND, 999);
5bfcd3 741         return calendar.getTime();
ba61c9 742     }
F 743
85234c 744     /**这天的最后时间,没有毫秒
C 745      * 日期2000-01-01变2000-01-01 23:59:59
746      */
747     public static Date dayToEndDateNoMillisecond(Date date) {
748         Calendar calendar = Calendar.getInstance();
749         calendar.setTime(date);
750         calendar.set(Calendar.HOUR_OF_DAY, 23);
751         calendar.set(Calendar.MINUTE, 59);
752         calendar.set(Calendar.SECOND, 59);
753         return calendar.getTime();
754     }
755
2e1c15 756     /**获取月份的天数
C 757      * @param date 时间
758      * @return 月份的天数
759      */
760     public static int getMonthDays(Date date) {
761         Calendar cal = Calendar.getInstance();
762         cal.setTime(date);
763         cal.set(Calendar.DATE, 1);
764         cal.roll(Calendar.DATE, -1);
765         return cal.getActualMaximum(Calendar.DATE);
766     }
578f7b 767     
2e1c15 768     /**获取月份的天数
C 769      * @param year 年份
770      * @param month 月份
771      * @return 月份的天数
772      */
773     public static int getMonthDays(int year, int month) {
774         Calendar cal = Calendar.getInstance();
775         cal.set(Calendar.YEAR, year);
776         cal.set(Calendar.MONTH, (month - 1));
777         cal.set(Calendar.DATE, 1);
778         cal.roll(Calendar.DATE, -1);
779         return cal.getActualMaximum(Calendar.DATE);
780     }
781
782     /**获取月份的天数
783      * @param yearMonth 年月
784      * @param format 时间格式
578f7b 785      * @return 返回
2e1c15 786      */
C 787     public static int getMonthDays(String yearMonth,String format) {
788         SimpleDateFormat sdf = new SimpleDateFormat(format);
789         Calendar calendar = Calendar.getInstance();
790         try {
791             calendar.setTime(sdf.parse(yearMonth));
792         } catch (ParseException e) {
793             e.printStackTrace();
794         }
795         calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
796         return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
797     }
5bfcd3 798
C 799     /**当天多少点前
800      * @param date 时间
801      * @param num 多少点前,不含当点前,24小时制
802      * @return 月份的天数
803      */
804     public static boolean getFrontMinute(Date date,int num) {
805         Calendar cal = Calendar.getInstance();
806         cal.setTime(date);
05b175 807         cal.set(Calendar.HOUR_OF_DAY, num-1);
5bfcd3 808         cal.set(Calendar.MINUTE, 59);
C 809         cal.set(Calendar.SECOND, 59);
810
811         return date.compareTo(cal.getTime()) < 1;
812     }
8782bc 813
F 814     /**
815      * 获得某个日期的当天某点
816      * 例如:2022-12-26 11:20:00 -> 2022-12-26 13:00:00
817      * @param num 24小时
818      */
819     public static String getHourDayTime(Date date,int num) {
820         Calendar calendar = Calendar.getInstance();
821         calendar.setTime(date);
822         calendar.set(Calendar.HOUR_OF_DAY, num);
823         calendar.set(Calendar.MINUTE, 0);
824         calendar.set(Calendar.SECOND, 0);
825         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
826         return sdf.format(calendar.getTime());
827     }
217258 828
F 829     /**
830      * 获取时间当月剩余天数
831      * */
832     public static Integer getMonthSurplus(Date date) {
833         Calendar month = Calendar.getInstance();
834         month.setTime(new Date());
835         month.set(Calendar.DATE, 1);
836         month.roll(Calendar.DATE, -1);
837         return month.getActualMaximum(Calendar.DATE) - Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
838     }
3a93e8 839
F 840     /**获取当前时间所在周的周一00:00:00*/
841     public static Date getMonday(Date date) {
842         Calendar calendar = Calendar.getInstance(Locale.CHINA);
843         calendar.setTime(date);
844         //以周一为首日
845         calendar.setFirstDayOfWeek(Calendar.MONDAY);
846         calendar.set(Calendar.HOUR_OF_DAY, 0);
847         calendar.set(Calendar.MINUTE, 0);
848         calendar.set(Calendar.SECOND, 0);
849         //周一
850         calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
851         return calendar.getTime();
852     }
736fa6 853
G 854     /**
855      * 根据出生年月日计算年龄
856      * @param birth
857      * @return
858      */
859     public static int getAge(Date birth) {
860         Calendar cal = Calendar.getInstance();
861         int thisYear = cal.get(Calendar.YEAR);
862         int thisMonth = cal.get(Calendar.MONTH);
863         int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
864
865         cal.setTime(birth);
866         int birthYear = cal.get(Calendar.YEAR);
867         int birthMonth = cal.get(Calendar.MONTH);
868         int birthdayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
869
870         int age = thisYear - birthYear;
871
872         // 未足月
873         if (thisMonth <= birthMonth) {
874             // 当月
875             if (thisMonth == birthMonth) {
876                 // 未足日
877                 if (dayOfMonth < birthdayOfMonth) {
878                     age--;
879                 }
880             } else {
881                 age--;
882             }
883         }
884         return age;
885     }
7381f4 886
F 887     /**
888      * 获取某天结束秒数
d55850 889      * @param dateTime      日期
F 890      * @param lateSecond    延迟秒数
7381f4 891      * @return
F 892      */
dca24a 893     public static long todayEndSecond(Date dateTime, Long lateSecond) {
d55850 894         if(dateTime == null){
F 895             dateTime = new Date();
7381f4 896         }
d55850 897         if(lateSecond == null){
F 898             lateSecond = 0L;
899         }
900         Date endTime = DateUtil.dayToEndDate(dateTime);
901         return differSecond(dateTime, endTime) + lateSecond;
7381f4 902     }
F 903
904     /**
905      * 计算2个实际相差秒数
906      * @param startTime 开始时间
907      * @param endTime   结束时间
908      * @return
909      */
dca24a 910     public static long differSecond(Date startTime, Date endTime) {
7381f4 911         if(startTime == null || endTime == null){
F 912             return 0L;
913         }
914         long sTime = startTime.getTime();
915         long eTime = endTime.getTime();
916         return (eTime - sTime) / 1000L;
917     }
5c5945 918 }