xyh
2021-06-18 aa9d5899deda2c9a9c4da111e89509136f1c4ec2
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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
package com.panzhihua.common.utlis;
 
import lombok.extern.slf4j.Slf4j;
 
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
 
@Slf4j
public class DateUtils {
 
 
    public static DateTimeFormatter format_ymdhms = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
 
    private static DateTimeFormatter format_ymdhmssss = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
 
    private static DateTimeFormatter format_ymds = DateTimeFormatter.ofPattern("yyyyMMdd");
 
    public static DateTimeFormatter format_ymd = DateTimeFormatter.ofPattern("yyyy-MM-dd");
 
    public static DateTimeFormatter format_ymdhms_string = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
 
    public static DateTimeFormatter format_ymdhms_no_signal = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
 
    public static SimpleDateFormat yyyyMMdd_format = new SimpleDateFormat("yyyy-MM-dd");
 
    public static SimpleDateFormat yyyy_MM_dd_format = new SimpleDateFormat("yyyy/MM/ddHH:mm:ss");
 
    public static SimpleDateFormat ymdhms_format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 
    public static DateTimeFormatter format_ymd_String = DateTimeFormatter.ofPattern("yyMMdd");
 
    public static SimpleDateFormat yyyyMM_format = new SimpleDateFormat("yyyy-MM");
 
 
 
    /**
     * 将日期对象格式化成指定的字符串格式
     *
     * @param date         日期对象
     * @param formatString 格式化字符串
     * @return String
     */
    public static String getDateFormatString(Date date, String formatString) {
 
        String dateString = "";
        SimpleDateFormat format = new SimpleDateFormat(formatString);
        if (date != null) {
            dateString = format.format(date);
        }
        return dateString;
    }
 
    /**
     * 字符串转date
     * @param date 时间戳
     * @param format format
     * @return 时间
     */
    public static Date stringToDate(String date,SimpleDateFormat format){
        try {
            if(StringUtils.isBlank(date)){
                return null;
            }
            return format.parse(date);
        } catch (ParseException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }
 
 
    /**
     * 字符串转date
     * @param date 时间戳
     * @return 时间
     */
    public static Date stringToDateStandard (String date){
        SimpleDateFormat format = ymdhms_format;
        if(StringUtils.isNotBlank(date)){
            if(date.split("-").length==3){
                if(date.split(":").length!=3){
                    format = yyyyMMdd_format;
                }
            }else if(date.split("-").length==2){
                format = new SimpleDateFormat("yyyy-MM");
            }
            try {
                return format.parse(date);
            } catch (ParseException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }
 
    /**
     * 字符串转date
     * @param date 时间戳
     * @return 时间
     */
    public static Date stringToDateStandardByYYYY_MM_dd (String date){
        SimpleDateFormat format = yyyy_MM_dd_format;
        if(StringUtils.isNotBlank(date)){
            try {
                return format.parse(date);
            } catch (ParseException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }
 
 
    /**
     * 获取当前时间
     * @return
     * @throws Exception
     */
    public static LocalDateTime getCurrentDate(){
        LocalDateTime now = LocalDateTime.now();
        return now;
    }
 
    /**
     * 获取当前时间字符串,格式为yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String getCurrentDateStr(){
        String nowtime=getCurrentDate().format(format_ymdhms);
        return nowtime;
    }
 
    public static String getCurrentDateymd(){
        String nowtime=getCurrentDate().format(format_ymdhms_string);
        return nowtime;
    }
 
    public static String getCurrentDateyymd(){
        String nowtime=getCurrentDate().format(format_ymd_String);
        return nowtime;
    }
 
    /**
     * 获取当前时间字符串,格式为yyyyMMdd
     * @return
     */
    public static String getCurrentDateString(){
        String nowtime=getCurrentDate().format(format_ymds);
        return nowtime;
    }
 
 
    /**
     * 获取当前时间字符串,格式为yyyy-MM-dd
     * @return
     */
    public static String getCurrentDateStrymd(){
        String nowtime=getCurrentDate().format(format_ymd);
        return nowtime;
    }
 
    public static String getCurrentDateStr_MS(){
        String nowtime=getCurrentDate().format(format_ymdhmssss);
        return nowtime;
    }
 
    /**
     * 获取当前时间long值
     * @return
     */
    public static long getCurrentDataLong(){
        return new Date().getTime();
    }
 
 
 
    /**
     * 获取当前时间
     * @param format
     * @return
     * @throws Exception
     */
    public static Date getCurrentDate(SimpleDateFormat format){
        String tempDate = format.format(new Date());
        try {
            return format.parse(tempDate);
        } catch (ParseException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }
 
    /**
     * @param date
     * @param newFormat
     * @return
     * @throws Exception
     */
    public static Date getDate(Date date, SimpleDateFormat newFormat) {
        String tempDate = newFormat.format(date);
        try {
            return newFormat.parse(tempDate);
        } catch (ParseException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }
 
    public static String format(Date date, SimpleDateFormat format) {
        if(null == date){
            return "";
        }
        return format.format(date);
    }
 
    /**
     * 将给定的时间秒转换为中文格式的时分秒
     *
     * @param second
     * @return
     */
    public static String formatSecond_ZH_HMS(Integer second) {
        String result = "0秒";
        if (null != second) {
            int hours = (int) (second / (60 * 60));
            int minutes = (int) (second / 60 - hours * 60);
            int seconds = (int) (second - minutes * 60 - hours * 60 * 60);
 
            String format;
            Object[] array;
 
            if (hours > 0) {
                format = "%1$,d时%2$,d分%3$,d秒";
                array = new Object[]{hours, minutes, seconds};
            } else if (minutes > 0) {
                format = "%1$,d分%2$,d秒";
                array = new Object[]{minutes, seconds};
            } else {
                format = "%1$,d秒";
                array = new Object[]{seconds};
            }
            result = String.format(format, array);
        }
        return result;
    }
 
    /**
     * 对日期进行加法操作
     *
     * @param date
     * @param days
     * @return
     */
    public static Date addDay(Date date, Integer days) {
        if (null != date) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.add(Calendar.DATE, days);
            return cal.getTime();
        }
        return null;
    }
 
    /**
     * 对日期进行加法操作
     *
     * @param date
     * @param month
     * @return
     */
    public static Date addMonth(Date date, Integer month) {
        if (null != date) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.add(Calendar.MONTH, month);
            return cal.getTime();
        }
        return null;
    }
 
    /**
     * 对日期进行加法操作
     * @param date
     * @param hours
     * @return
     */
    public static Date addHours(Date date, Integer hours) {
        if (null != date) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.add(Calendar.HOUR_OF_DAY, hours);
            return cal.getTime();
        }
        return null;
    }
 
    /**
     * 对日期的分钟进行加法操作
     * @param date
     * @param minutes
     * @return
     */
    public static Date addMinutes(Date date, Integer minutes) {
        if (null != date) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.add(Calendar.MINUTE, minutes);
            return cal.getTime();
        }
        return null;
    }
 
    /**
     * 比较两个日期的大小
     *
     * @param date1
     * @param date2
     * @return 日期相等,返回0;前者大,返回1;后者大,返回-1;
     */
    public static int dateCompare(Date date1, Date date2) {
        Calendar c1 = Calendar.getInstance();
        c1.setTime(date1);
 
        Calendar c2 = Calendar.getInstance();
        c2.setTime(date2);
 
        return c1.compareTo(c2);
    }
 
    /**
     * 判断当前日期是否在指定日期区域内(包含起止日期) [startDate,endDate]
     * <p/>
     * [null,null] --> return 1;
     *
     * @param now
     * @param startDate
     * @param endDate
     * @return -1:超出并小于startDate;0:在范围内;1:超出并大于endDate;
     */
    public static int validateDate(Date now, Date startDate, Date endDate) {
        if (null == startDate && null == endDate) {
            return 0;
        }
        if (null == startDate) {
            //当前时间大于endDate
            if (dateCompare(now, endDate) == 1) {
                return 1;
            } else {
                return 0;
            }
        } else {
            if (null == endDate) {
                //当前时间小于startDate
                if (dateCompare(startDate, now) == 1) {
                    return -1;
                } else {
                    return 0;
                }
            } else {
                if (dateCompare(startDate, now) == 1) {
                    return -1;
                } else if (dateCompare(now, endDate) == 1) {
                    return 1;
                } else {
                    return 0;
                }
            }
        }
    }
 
    /**
     * 计算两个日期先差多少秒
     * @param pre 前一个日期
     * @param after 后一个日期
     * @return
     */
    public static long calTimeDifference(Date pre,Date after){
        return (after.getTime()-pre.getTime())/1000;
    }
 
 
 
    /**
     * 计算两个时间相差的天数
     * @param stardate
     * @param enddate
     * @return
     */
    public static int getHour(Date stardate,Date enddate){
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        long diff=enddate.getTime()-stardate.getTime();
 
        long day = diff / nd;
 
        long hour = diff % nd / nh;
 
        long min = diff % nd % nh / nm;
 
        return (int)day;
    }
 
    /**
     * 结束时间与当前时间计算相差的月数
     * @param enddate
     * @return
     */
    public static int getMonthSpace(String enddate, DateTimeFormatter format) {
        LocalDate formatted = LocalDate.parse(enddate,format);
        LocalDate today = LocalDate.now();
        Period per = Period.between(today,formatted);
        if(per.getYears() == 0 ){
            return per.getMonths();
        }else if(per.getYears() > 0 ){
            return (per.getYears() * 12) + per.getMonths();
        }
 
        return (per.getYears() * 12) + per.getMonths();
    }
 
    /**
     * 获取某个时间段之前的时间点
     * @return
     */
    public static String getSomeTimeStapStr(String sign){
        LocalDateTime beginDateTime =null;
        if (sign.equals("day")) {//一天前时间
            beginDateTime =LocalDateTime.now().minusDays(1L);
        }else if(sign.equals("week")){
            beginDateTime =LocalDateTime.now().minusWeeks(1L);
        }else if (sign.equals("month")) {
            beginDateTime =LocalDateTime.now().minusMonths(1L);
        }else if (sign.equals("year")) {
            beginDateTime =LocalDateTime.now().minusYears(1L);
        }
        String beginTime=beginDateTime.format(format_ymdhms);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            long timestap = sdf.parse(beginTime).getTime();
            beginTime = timestap+"";
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return beginTime;
    }
 
    public static String getSomeTimeDateStr(String sign){
        String beginTime="";
        LocalDateTime beginDateTime =null;
        if (sign.equals("day")) {//一天前时间
            beginDateTime =LocalDateTime.now().minusDays(1L);
        }else if(sign.equals("week")){
            beginDateTime =LocalDateTime.now().minusWeeks(1L);
        }else if (sign.equals("month")) {
            beginDateTime =LocalDateTime.now().minusMonths(1L);
        }else if (sign.equals("year")) {
            beginDateTime =LocalDateTime.now().minusYears(1L);
        }
        if (beginDateTime!=null) {
            beginTime = beginDateTime.format(format_ymdhms);
        }
        return beginTime;
    }
 
 
    /**
     * 获取失效时间点(在什么时间失效)
     * @param minutes       有效分钟数
     * @return
     */
    public static LocalDateTime getInvalidLocalDateTime(Long minutes){
        return LocalDateTime.now().minusMinutes(minutes);
    }
 
 
    /**
     * 获取当前年份
     * @return
     */
    public static String getCurrentYear(){
        Calendar instance = Calendar.getInstance();
        return instance.get(Calendar.YEAR)+"";
    }
 
 
    /**
     * 获取当前月份
     * @return
     */
    public static String getCurrentMonth(){
        Calendar instance = Calendar.getInstance();
        int month = instance.get(Calendar.MONTH);
        String result;
        if (month < 10){
            result = "0" + month;
        }else {
            result = month+"";
        }
        return result;
    }
 
    /** 获取无符号的当前时间
     * @return
     */
    public static String getCurrentDateStrNoSignal(){
        return getCurrentDate().format(format_ymdhms_no_signal);
    }
 
 
    /**
     * 获取前几个小时的时间
     * @param hours
     * @return
     */
    public static String getNextHourDateStrNoSignal(long hours){
        return LocalDateTime.now().plusHours(hours).format(format_ymdhms_no_signal);
    }
 
 
    public static Date localDateTimeToDate(LocalDateTime localDateTime) {
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDateTime.atZone(zone).toInstant();
        return Date.from(instant);
    }
 
    /**
     * 小时取整
     * @param date
     * @param hour
     * @return
     */
    public static Date integralHour(Date date,Integer hour) {
        if(date==null||hour==null){
            return null;
        }
        long l = date.getTime()- 1;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:00:00");
        try {
            Date parse = sdf.parse(sdf.format(new Date(l)));
            return addHours(parse, hour);
        } catch (ParseException e) {
            return null;
        }
    }
 
    /**
     * 分钟取整
     * 以十分钟为单位 ,去除尾端,加上 参数  46->40+minutes*10
     * @param date
     * @param minutes
     * @return
     */
    public static Date integral10Min(Date date,Integer minutes) {
        if(date==null||minutes==null){
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int i = cal.get(Calendar.MINUTE);//获取分钟
        if(i%10==0){
            minutes--;
        }
        minutes=minutes*10-i%10;
        long l = date.getTime()- 1;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:00");
        try {
            Date parse = sdf.parse(sdf.format(new Date(l)));
            return addMinutes(parse, minutes);
        } catch (ParseException e) {
            return null;
        }
    }
 
    /**
     * 天数取整
     * @param date
     * @param day
     * @return
     */
    public static Date integralDay(Date date,Integer day) {
        if(date==null||day==null){
            return null;
        }
        long l = date.getTime()- 1;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        try {
            Date parse = sdf.parse(sdf.format(new Date(l)));
            return addDay(parse, day);
        } catch (ParseException e) {
            return null;
        }
    }
 
    /**
     * 日期相加减
     * @param time
     *             时间字符串 yyyy-MM-dd HH:mm:ss
     * @param num
     *             加的数,-num就是减去
     * @return
     *             减去相应的数量的年的日期
     * @throws ParseException
     */
    public static Date yearAddNum(Date time, Integer num) {
        //SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //Date date = format.parse(time);
 
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.add(Calendar.YEAR, num);
        Date newTime = calendar.getTime();
        return newTime;
    }
 
    public static boolean isValidDate(String str) {
        boolean convertSuccess=true;
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
        try {
            format.setLenient(false);
            format.parse(str);
        } catch (ParseException e) {
            convertSuccess=false;
        }
        return convertSuccess;
    }
 
    public static Date toValidDate(String str) {
        Date date = null;
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
        try {
            format.setLenient(false);
            date = format.parse(str);
        } catch (ParseException e) {
            log.error("党员导入日期格式错误");
            date = new Date();
        }
        return date;
    }
 
 
    /**
     * 将日期对象格式化成yyyy-MM-dd格式的字符串
     *
     * @param date         日期对象
     * @return String
     */
    public static String getDateStringYMD(Date date) {
 
        String dateString = "";
        if (date != null) {
            dateString = yyyyMMdd_format.format(date);
        }
        return dateString;
    }
 
    /**
     * 获取当前月第一天
     * @return
     */
    public static Date getFirstDayOfMonth() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH,0);
        int actualMinimum = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY),actualMinimum,00,00,00);
        return cal.getTime();
    }
 
    /**
     * 获取当前年月的第一天时间
     * @param year  年份
     * @param month 月份
     * @return  时间
     */
    public static Date getYearMonthStart(Integer year,Integer month) {
        Calendar cal = Calendar.getInstance();
        // 设置年份
        cal.set(Calendar.YEAR, year);
        // 设置月份
        cal.set(Calendar.MONTH, month - 1);
        // 获取某月最小天数
        int firstDay = cal.getMinimum(Calendar.DATE);
        // 设置日历中月份的最小天数
        cal.set(Calendar.DAY_OF_MONTH, firstDay);
        int actualMinimum = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY),actualMinimum,00,00,00);
        return cal.getTime();
    }
 
    /**
     * 获取当前年月的最后一天时间
     * @param year  年份
     * @param month 月份
     * @return  时间
     */
    public static Date getYearMonthEnd(Integer year,Integer month) {
        Calendar cal = Calendar.getInstance();
        // 设置年份
        cal.set(Calendar.YEAR, year);
        // 设置月份
        cal.set(Calendar.MONTH, month - 1);
        // 获取某月最大天数
        int lastDay = cal.getMaximum(Calendar.DATE);
        // 设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        int actualMinimum = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY),actualMinimum,00,00,00);
        return cal.getTime();
    }
 
    public static Date getDateM(Date start, int min) {
        Calendar date1 = Calendar.getInstance();
        date1.setTime(start);
        date1.add(Calendar.MONTH, min);
        return date1.getTime();
    }
 
    public static Date parseDateYM(String str) {
        try {
            return yyyyMM_format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
 
 
    public static int getMonth(Date date){
        Calendar instance = Calendar.getInstance();
        instance.setTime(date);
        return instance.get(Calendar.MONTH);
    }
 
    public static boolean before(Date start, Date end){
        Calendar date1 = Calendar.getInstance();
        date1.setTime(start);
        Calendar date2 = Calendar.getInstance();
        date2.setTime(end);
        return date1.before(date2);
    }
 
 
 
 
    public static void main(String[]args)throws Exception{
        isValidDate("2020/12/4");
//        Date date= new Date();
//        Date after = new Date();
//        System.out.println(calTimeDifference(date,after));
        System.out.println(getYearMonthStart(2021,2));
 
    }
}