admin
2020-05-20 98b1a0affd69bbe63223c21fdd2c404e8bedfccb
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
package org.yeshi.utils;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
 
import org.yeshi.utils.entity.DateInfo;
 
public class DateUtil {
 
    public static String dateDiff(String startTime, String endTime) {
 
        String datatime = 0 + "天" + 0 + "小时" + 0 + "分钟";
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
 
        try {
            long nm = 1000 * 60;// 一分钟的毫秒数
            long nh = 1000 * 60 * 60;// 一小时的毫秒数
            long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
 
            // 获得两个时间的毫秒时间差异
            long diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
 
            long day = diff / nd;// 计算差多少天
            long hour = diff % nd / nh;// 计算差多少小时
            long min = diff % nd % nh / nm;// 计算差多少分钟
 
            datatime = day + "天" + hour + "小时" + min + "分钟";
 
        } catch (ParseException e) {
            e.printStackTrace();
        }
 
        return datatime;
    }
 
    public static String dateDiff2(Date startTime, Date endTime) throws Exception {
 
        String datatime = "0分";
 
        long nm = 1000 * 60;// 一分钟的毫秒数
        long nh = 1000 * 60 * 60;// 一小时的毫秒数
        long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
 
        // 获得两个时间的毫秒时间差异
        long diff = endTime.getTime() - startTime.getTime();
        long day = diff / nd;// 计算差多少天
        long hour = diff % nd / nh;// 计算差多少小时
        long min = diff % nd % nh / nm;// 计算差多少分钟
 
        if (day > 0) {
            datatime = day + "天" + hour + "时" + min + "分";
        } else {
            if (hour > 0) {
                datatime = hour + "时" + min + "分";
            } else {
                if (min < 0)
                    min = 0;
                datatime = min + "分";
            }
        }
        return datatime;
    }
 
    public static DateInfo dateDiff3(long startTime, long endTime) throws Exception {
        long nm = 1000 * 60;// 一分钟的毫秒数
        long nh = 1000 * 60 * 60;// 一小时的毫秒数
        long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
 
        // 获得两个时间的毫秒时间差异
        long diff = endTime - startTime;
 
        int day = (int) (diff / nd);// 计算差多少天
        long hour = diff % nd / nh;// 计算差多少小时
        long minute = diff % nd % nh / nm;// 计算差多少分钟
        long second = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - minute * 60); // 计算差多少秒
 
        DateInfo dateInfo = new DateInfo();
        dateInfo.setDay(day);
        dateInfo.setHour(hour);
        dateInfo.setMinute(minute);
        dateInfo.setSecond(second);
        return dateInfo;
    }
 
    public static String dateDiff4(Date startTime, Date endTime) throws Exception {
 
        String datatime = "0分";
 
        long nm = 1000 * 60;// 一分钟的毫秒数
        long nh = 1000 * 60 * 60;// 一小时的毫秒数
        long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
 
        // 获得两个时间的毫秒时间差异
        long diff = endTime.getTime() - startTime.getTime();
        long day = diff / nd;// 计算差多少天
        long hour = diff % nd / nh;// 计算差多少小时
        long min = diff % nd % nh / nm;// 计算差多少分钟
 
        if (day > 0) {
            datatime = day + "天";
        } else {
            if (hour > 0) {
                datatime = hour + "时";
            } else {
                if (min < 0)
                    min = 0;
                datatime = min + "分";
            }
        }
        return datatime;
    }
 
    public static String dateDiff5(Date startTime, Date endTime) throws Exception {
        long nm = 1000 * 60;// 一分钟的毫秒数
        long nh = 1000 * 60 * 60;// 一小时的毫秒数
        long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
 
        // 获得两个时间的毫秒时间差异
        long diff = endTime.getTime() - startTime.getTime();
        long day = diff / nd;// 计算差多少天
        long hour = diff % nd / nh;// 计算差多少小时
        long min = diff % nd % nh / nm;// 计算差多少分钟
        long second = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60); // 计算差多少秒
 
        String datatime = "";
        if (day > 0) {
            datatime = day + "天";
        } else if (hour > 0) {
            datatime = hour + "小时";
        } else if (min > 0) {
            datatime = min + "分钟";
        } else if (second > 0) {
            datatime = second + "秒";
        }
        return datatime;
    }
 
    public static long dateDiffMin(Date startTime, Date endTime) throws Exception {
        long nm = 1000 * 60;// 一分钟的毫秒数
        long nh = 1000 * 60 * 60;// 一小时的毫秒数
        long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
 
        // 获得两个时间的毫秒时间差异
        long diff = endTime.getTime() - startTime.getTime();
        long day = diff / nd;// 计算差多少天
        long hour = diff % nd / nh;// 计算差多少小时
        long min = diff % nd % nh / nm + day * 24 * 60 + hour * 60;// 计算差多少分钟
        return min;
    }
 
    /**
     * 通过时间秒毫秒数判断两个时间的间隔
     * 
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDaysByMillisecond(Date start, Date end) {
        return (int) ((end.getTime() - start.getTime()) / (1000 * 3600 * 24));
    }
 
    public String transferLongToDate(String dateFormat, Long millSec) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(millSec);
        return sdf.format(date);
    }
 
    /**
     * 验证是否属于同一天
     * 
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isSameDay(Date date1, Date date2) {
        if (date1 != null && date2 != null) {
            Calendar cal1 = Calendar.getInstance();
            cal1.setTime(date1);
            Calendar cal2 = Calendar.getInstance();
            cal2.setTime(date2);
            return isSameDay(cal1, cal2);
        } else {
            return false;
        }
    }
 
    /**
     * 验证是否属于同一天
     * 
     * @param cal1
     * @param cal2
     * @return
     */
    public static boolean isSameDay(Calendar cal1, Calendar cal2) {
        if (cal1 != null && cal2 != null) {
            return cal1.get(0) == cal2.get(0) && cal1.get(1) == cal2.get(1) && cal1.get(6) == cal2.get(6);
        } else {
            return false;
        }
    }
 
    /**
     * 判断是否同一个月
     * 
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isSameMonth(Date date1, Date date2) {
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(date1);
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(date2);
        int year1 = calendar1.get(Calendar.YEAR);
        int year2 = calendar2.get(Calendar.YEAR);
        int month1 = calendar1.get(Calendar.MONTH);
        int month2 = calendar2.get(Calendar.MONTH);
        System.out.println(year1 + "  " + month1);
        System.out.println(year2 + "  " + month2);
        return calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR)
                && calendar1.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH);
    }
 
    /**
     * 指定日期加上天数后的日期
     * 
     * @param num     增加的天数
     * @param nowDate 创建时间
     * @return
     * @throws ParseException
     */
    public static String plusDay(int num, String nowDate) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date currdate = format.parse(nowDate);
 
        Calendar ca = Calendar.getInstance();
        ca.setTime(currdate);
        ca.add(Calendar.DATE, num);
 
        return format.format(ca.getTime());
    }
 
    /**
     * 指定日期加上天数后的日期
     * 
     * @param num     增加的天数
     * @param nowDate 创建时间
     * @return
     * @throws ParseException
     */
    public static Date plusDayReturnDate(int num, String nowDate) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date currdate = format.parse(nowDate);
        Calendar ca = Calendar.getInstance();
        ca.setTime(currdate);
        ca.add(Calendar.DATE, num);
        return ca.getTime();
    }
 
    /**
     * 指定日期加上天数后的日期
     * 
     * @param num     增加的天数
     * @param nowDate 创建时间
     * @return
     * @throws ParseException
     */
    public static String plusDay(int num, Date currdate) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
 
        Calendar ca = Calendar.getInstance();
        ca.setTime(currdate);
        ca.add(Calendar.DATE, num);
 
        return format.format(ca.getTime());
    }
 
    /**
     * 指定日期加上天数后的日期
     * 
     * @param num     增加的天数
     * @param nowDate 创建时间
     * @return
     * @throws ParseException
     */
    public static Date plusDayDate(int num, Date currdate) {
        Calendar ca = Calendar.getInstance();
        ca.setTime(currdate);
        ca.add(Calendar.DATE, num);
        return ca.getTime();
    }
 
    /**
     * 加上月份
     * 
     * @param currdate
     * @param num
     * @return
     */
    public static Date plusMonths(Date currdate, int num) {
        Calendar ca = Calendar.getInstance();
        ca.setTime(currdate);
        ca.add(Calendar.MONTH, num);
        return ca.getTime();
    }
 
    /**
     * 加上年份
     * 
     * @param currdate
     * @param num
     * @return
     */
    public static Date plusYears(Date currdate, int num) {
        Calendar ca = Calendar.getInstance();
        ca.setTime(currdate);
        ca.add(Calendar.YEAR, num);
        return ca.getTime();
    }
 
    /**
     * 减去月份
     * 
     * @param currdate
     * @param num
     * @return
     */
    public static Date reduceMonth(Date currdate, int num) {
        Calendar ca = Calendar.getInstance();
        ca.setTime(currdate);
        ca.add(Calendar.MONTH, -num);
        return ca.getTime();
    }
    
    public static Date reduceDay(Date currdate, int num) {
        Calendar ca = Calendar.getInstance();
        ca.setTime(currdate);
        ca.add(Calendar.DATE, -num);
        return ca.getTime();
    }
    
    /**
     * 指定日期减去天数后的日期
     * 
     * @param num     减去的天数
     * @param nowDate 创建时间
     * @return
     * @throws ParseException
     */
    public static String reduceDay(int num, String nowDate) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date currdate = format.parse(nowDate);
 
        Calendar ca = Calendar.getInstance();
        ca.setTime(currdate);
        ca.add(Calendar.DATE, -num); // 日期减 如果不够减会将月变动
 
        return format.format(ca.getTime());
    }
 
    /**
     * 指定日期减去天数后的日期
     * 
     * @param num     减去的天数
     * @param nowDate 创建时间
     * @return
     * @throws ParseException
     */
    public static String reduceDay2(int num, Date date) throws ParseException {
        // 设置要获取到什么样的时间
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        // 获取String类型的时间
        String date_str = format.format(date);
        Date currdate = format.parse(date_str);
 
        Calendar ca = Calendar.getInstance();
        ca.setTime(currdate);
        ca.add(Calendar.DATE, -num); // 日期减 如果不够减会将月变动
 
        return format.format(ca.getTime());
    }
 
    /**
     * 指定日期减去天数后的日期
     * 
     * @param num     减去的天数
     * @param nowDate 创建时间
     * @return
     * @throws ParseException
     */
    public static Date reduceDay(int num, Date date) throws ParseException {
        // 设置要获取到什么样的时间
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        // 获取String类型的时间
        String date_str = format.format(date);
        Date currdate = format.parse(date_str);
 
        Calendar ca = Calendar.getInstance();
        ca.setTime(currdate);
        ca.add(Calendar.DATE, -num); // 日期减 如果不够减会将月变动
 
        return ca.getTime();
    }
 
    /**
     * 指定日期减去天数后的日期
     * 
     * @param num     减去的天数
     * @param nowDate 创建时间
     * @return
     * @throws ParseException
     */
    public static String reduceDayTostring(int num, Date date) throws ParseException {
        // 设置要获取到什么样的时间
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        // 获取String类型的时间
        String date_str = format.format(date);
        Date currdate = format.parse(date_str);
 
        Calendar ca = Calendar.getInstance();
        ca.setTime(currdate);
        ca.add(Calendar.DATE, -num); // 日期减 如果不够减会将月变动
        return format.format(ca.getTime());
    }
 
    /**
     * 计算两个日期之间相差的天数
     * 
     * @param smdate 较小的时间
     * @param bdate  较大的时间
     * @return 相差天数
     * @throws ParseException calendar 对日期进行时间操作 getTimeInMillis() 获取日期的毫秒显示形式
     */
    public static int daysBetween(Date minDate, Date maxDate) throws ParseException {
        Calendar cal = Calendar.getInstance();
        cal.setTime(minDate);
        long minTime = cal.getTimeInMillis();
        cal.setTime(maxDate);
        long maxTime = cal.getTimeInMillis();
        long between_days = (maxTime - minTime) / (1000 * 3600 * 24);
        return Integer.parseInt(String.valueOf(between_days));
    }
 
    /**
     * 字符串日期格式的计算
     * 
     * @param smdate
     * @param bdate
     * @return 单位天数
     * @throws ParseException
     */
    public static int daysBetween2(Date minDate, Date maxDate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return daysBetween(sdf.format(minDate), sdf.format(maxDate));
    }
 
    /**
     * 字符串日期格式的计算
     * 
     * @param smdate
     * @param bdate
     * @return
     * @throws ParseException
     */
    public static int daysBetween(String minDate, String maxDate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(sdf.parse(minDate));
        long minTime = cal.getTimeInMillis();
        cal.setTime(sdf.parse(maxDate));
        long maxTime = cal.getTimeInMillis();
        long between_days = (maxTime - minTime) / (1000 * 3600 * 24);
        return Integer.parseInt(String.valueOf(between_days));
    }
 
    /**
     * 返回中间日期
     * 
     * @param startTime
     * @param endTime
     * @return
     * @throws Exception
     */
    public static List<String> dayFactory(String startTime, String endTime) throws Exception {
 
        List<String> listDate = new ArrayList<String>();
 
        String plusDay = "";
        for (int i = 0; i < 1000; i++) {
            if (i == 0) {
                plusDay = startTime;
            } else {
                plusDay = DateUtil.plusDay(i, startTime);
            }
 
            listDate.add(plusDay);
 
            if (plusDay.equals(endTime)) {
                break; // 时间结束
            }
        }
        return listDate;
    }
 
    /**
     * 随机减去几分钟
     * 
     * @param date
     * @param min  随机添加的分钟数
     * @return
     */
    public static Date reduceRandomMinute(Date date, int min) {
        long rand = (long) (Math.random() * 1000 * 60 * min);
        long time = date.getTime() - rand;
        return new Date(time);
    }
 
    /**
     * 计算两时间月差
     * 
     * @param startDate <String>
     * @param endDate   <String>
     * @return int
     * @throws ParseException
     */
    public static int getMonthSpace(String startDate, String endDate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        return getMonthSpace(sdf.parse(startDate), sdf.parse(endDate));
    }
 
    /**
     * 计算两时间月差
     * 
     * @param startDate <String>
     * @param endDate   <String>
     * @return int
     * @throws ParseException
     */
    public static int getMonthSpace(Date startDate, Date endDate) {
        Calendar start = Calendar.getInstance();
        Calendar end = Calendar.getInstance();
        start.setTime(startDate);
        end.setTime(endDate);
        int result = end.get(Calendar.MONTH) - start.get(Calendar.MONTH);
        int month = (end.get(Calendar.YEAR) - start.get(Calendar.YEAR)) * 12;
        return Math.abs(month + result);
    }
 
    public static Date getTodayStartTime() {
        Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.HOUR_OF_DAY, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        return todayStart.getTime();
    }
 
    public static Date getTodayEndTime() {
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.set(Calendar.HOUR_OF_DAY, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MILLISECOND, 999);
        return todayEnd.getTime();
    }
 
    /**
     * 获取指定年月的第一天
     * 
     * @param yearMonth
     * @return
     */
    public static String getFirstDayOfMonth(String yearMonth) {
        int year = Integer.parseInt(yearMonth.split("-")[0]); // 年
        int month = Integer.parseInt(yearMonth.split("-")[1]); // 月
        return getFirstDayOfMonth(year, month);
    }
 
    /**
     * 获取指定年月的第一天
     * 
     * @param year
     * @param month
     * @return
     */
    public static String getFirstDayOfMonth(int year, int 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);
        // 格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(cal.getTime());
    }
 
    /**
     * 获取指定年月的最后一天
     * 
     * @param yearMonth
     * @return
     */
    public static String getLastDayOfMonth(String yearMonth) {
        int year = Integer.parseInt(yearMonth.split("-")[0]); // 年
        int month = Integer.parseInt(yearMonth.split("-")[1]); // 月
        return getLastDayOfMonth(year, month);
    }
 
    /**
     * 获取指定年月的最后一天
     * 
     * @param year
     * @param month
     * @return
     */
    public static String getLastDayOfMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        // 设置年份
        cal.set(Calendar.YEAR, year);
        // 设置月份
        cal.set(Calendar.MONTH, month - 1);
        // 获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DATE);
        // 设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        // 格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(cal.getTime());
    }
 
    public static void main(String[] args) throws ParseException {
        System.out.println(getMonthSpace("2012-02", "2012-02"));
    }
 
}