yanghb
2024-12-17 1287337fd0b0c156ec79712f9a600ebeffefe3a6
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
package com.zzg.common.utils;
 
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
 
 
/**
 * Description:日期工具类
 *
 * @author lihaipeng
 * @create 2020-08-12
 */
public class DateUtil {
 
    /**
     * 仅显示年月日,例如 2015-08-11.
     */
    public static final String DATE_FORMAT = "yyyy-MM-dd";
    /**
     * 显示年月日时分秒,例如 2015-08-11 09:51:53.
     */
    public static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
 
    /**
     * 仅显示时分秒,例如 09:51:53.
     */
    public static final String TIME_FORMAT = "HH:mm:ss";
 
    /**
     * 每天的毫秒数 8640000.
     */
    public static final long MILLISECONDS_PER_DAY = 86400000L;
 
    /**
     * 每周的天数.
     */
    public static final long DAYS_PER_WEEK = 7L;
 
    /**
     * 每小时毫秒数.
     */
    public static final long MILLISECONDS_PER_HOUR = 3600000L;
 
    /**
     * 每分钟秒数.
     */
    public static final long SECONDS_PER_MINUTE = 60L;
 
    /**
     * 每小时秒数.
     */
    public static final long SECONDS_PER_HOUR = 3600L;
 
    /**
     * 每天秒数.
     */
    public static final long SECONDS_PER_DAY = 86400L;
 
    /**
     * 每个月秒数,默认每月30天.
     */
    public static final long SECONDS_PER_MONTH = 2592000L;
 
    /**
     * 每年秒数,默认每年365天.
     */
    public static final long SECONDS_PER_YEAR = 31536000L;
 
    /**
     * 常用的时间格式.
     */
    public static String[] parsePatterns = {"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm"};
 
    /**
     * 得到当前日期字符串.
     *
     * @return String 日期字符串,例如2015-08-11
     * @since 1.0
     */
    public static String getDateByParseOne() {
        return getDate(parsePatterns[0]);
    }
 
    /**
     * 获取当前时间指定格式下的字符串.
     *
     * @param pattern 转化后时间展示的格式,例如"yyyy-MM-dd","yyyy-MM-dd HH:mm:ss"等
     * @return String 格式转换之后的时间字符串.
     * @since 1.0
     */
    public static String getDate(String pattern) {
        return DateFormatUtils.format(new Date(), pattern);
    }
 
    /**
     * 获取日期时间字符串
     *
     * @param date    需要转化的日期时间
     * @param pattern 时间格式,例如"yyyy-MM-dd" "HH:mm:ss" "E"等
     * @return String 格式转换后的时间字符串
     * @since 1.0
     */
    public static String formatDate(Date date, String pattern) {
        return DateFormatUtils.format(date, pattern);
    }
 
    /**
     * 将日期型字符串转换为日期格式.
     * 支持的日期字符串格式包括"yyyy-MM-dd","yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm",
     * "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm"
     *
     * @param dataStr
     * @return Date
     * @since 1.0
     */
    public static Date formatDate(String dataStr, String pattern) {
        if (dataStr == null) {
            return null;
        }
        try {
            return DateUtils.parseDate(dataStr, pattern);
        } catch (ParseException e) {
            return null;
        }
    }
 
    /**
     * 将日期型字符串转换为日期格式.
     * 支持的日期字符串格式包括"yyyy-MM-dd","yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm",
     * "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm"
     *
     * @param str
     * @return Date
     * @since 1.0
     */
    public static Date parseDate(Object str) {
        if (str == null) {
            return null;
        }
        try {
            return DateUtils.parseDate(str.toString(), parsePatterns);
        } catch (ParseException e) {
            return null;
        }
    }
 
    /**
     * 计算两个日期之间相差天数.
     *
     * @param start 计算开始日期
     * @param end   计算结束日期
     * @return long 相隔天数
     * @since 1.0
     */
    public static long getDaysBetween(Date start, Date end) {
        // 将指定日期转换为yyyy-MM-dd格式
        start = DateUtil.parseDate(DateUtil.formatDate(start, DateUtil.DATE_FORMAT));
        // 当前日期转换为yyyy-MM-dd格式
        end = DateUtil.parseDate(DateUtil.formatDate(end, DateUtil.DATE_FORMAT));
        long diff = 0;
        if (null != start && null != end) {
            diff = (end.getTime() - start.getTime()) / DateUtil.MILLISECONDS_PER_DAY;
        }
        return diff;
    }
    public static long getDaysBetween(String start, Date end) {
        // 将指定日期转换为yyyy-MM-dd格式
       Date startDate = parseDate(start);
        // 当前日期转换为yyyy-MM-dd格式
        end = DateUtil.parseDate(DateUtil.formatDate(end, DateUtil.DATE_FORMAT));
        long diff = 0;
        if (null != start && null != end) {
            diff = (end.getTime() - startDate.getTime()) / DateUtil.MILLISECONDS_PER_DAY;
        }
        return diff;
    }
 
    /**
     * 将时间戳转换为时间
     *
     * @param s
     * @return
     * @throws Exception
     */
    public static String stampToTime(String s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long(s);
        //将时间戳转换为时间
        Date date = new Date(lt);
        //将时间调整为yyyy-MM-dd HH:mm:ss时间样式
        res = simpleDateFormat.format(date);
        return res;
    }
 
    /**
     * 将字符串时间转换为long类型时间戳
     */
 
    public static long dateToStamp(String s, String pattern) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        Date date = simpleDateFormat.parse(s);
        long ts = date.getTime();
        return date.getTime();
    }
 
    /**
     * 判断当前时间是否在指定时间5分钟内
     *
     * @param isDate
     * @return
     */
    public static boolean judgeDate(Date isDate) {
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        Calendar c3 = Calendar.getInstance();
        c1.setTime(isDate);//要判断的日期
        c2.setTime(new Date());//初始日期
        c3.setTime(new Date());//也给初始日期 把分钟加五
        c3.add(Calendar.MINUTE, 5);
        c2.add(Calendar.MINUTE, -5);//减去五分钟
        if (c1.after(c2) && c1.before(c3)) {
            //5分钟内
            return false;
        } else {
            //5分钟外
            return true;
        }
    }
 
    private static final SimpleDateFormat YYYY_MM_DD = new SimpleDateFormat("yyyy-MM-dd");
    private static final SimpleDateFormat YYYY_MM_DD_TO_CHINESE = new SimpleDateFormat("yyyy年MM月dd日");
 
    /**
     * 获得该月第一天
     *
     * @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.getActualMinimum(Calendar.DAY_OF_MONTH);
        // 设置日历中月份的最小天数
        cal.set(Calendar.DAY_OF_MONTH, firstDay);
        // 格式化日期
        String firstDayOfMonth = YYYY_MM_DD.format(cal.getTime());
        return firstDayOfMonth;
    }
 
    /**
     * 获得该月最后一天
     *
     * @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.DAY_OF_MONTH);
        // 设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        // 格式化日期
        String lastDayOfMonth = YYYY_MM_DD.format(cal.getTime());
        return lastDayOfMonth;
    }
 
    /**
     * 日期转换成字符串(yyyy-MM-dd)
     *
     * @param date
     * @return str
     */
    public static String transDateToStr(Date date) {
        String str = YYYY_MM_DD.format(date);
        return str;
    }
 
 
    /**
     * 日期转换成字符串(yyyy年MM月dd日)
     *
     * @param date
     * @return str
     */
    public static String transDateToChineseStr(Date date) {
        return YYYY_MM_DD_TO_CHINESE.format(date);
    }
 
 
    /**
     * 日期转换成字符串(自定义转换格式)
     *
     * @param date
     * @return str
     */
    public static String transDateToStr(String pattern, Date date) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(date);
    }
 
    /**
     * 字符串转换成日期(yyyy-MM-dd)
     *
     * @param str
     * @return date
     */
    public static Date transStrToDate(String str) {
        Date date = null;
        try {
            date = YYYY_MM_DD.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
 
    /**
     * 字符串转换成日期(自定义转换格式)
     *
     * @param pattern
     * @param str
     */
    public static Date transStrToDate(String pattern, String str) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        Date date = null;
        try {
            date = format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
 
    /**
     * 获取某日期区间的所有日期 日期倒序
     *
     * @param startDate  开始日期
     * @param endDate    结束日期
     * @param dateFormat 日期格式
     * @return 区间内所有日期
     */
    public static List<String> getPerDaysByStartAndEndDate(String startDate, String endDate, String dateFormat) {
        SimpleDateFormat format = new SimpleDateFormat(dateFormat);
        try {
            Date sDate = format.parse(startDate);
            Date eDate = format.parse(endDate);
            long start = sDate.getTime();
            long end = eDate.getTime();
            if (start > end) {
                return null;
            }
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(eDate);
            List<String> res = new ArrayList<String>();
            while (end >= start) {
                res.add(format.format(calendar.getTime()));
                calendar.add(Calendar.DAY_OF_MONTH, -1);
                end = calendar.getTimeInMillis();
            }
            return res;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
 
    /**
     * 日期转星期
     *
     * @param datetime
     * @return
     */
    public static String dateToWeek(String datetime) {
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        // 获得一个日历
        Calendar cal = Calendar.getInstance();
        Date datet = null;
        try {
            datet = YYYY_MM_DD.parse(datetime);
            cal.setTime(datet);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        // 指示一个星期中的某天。
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) {
            w = 0;
        }
        return weekDays[w];
    }
 
    /***
     * 日期转换cron表达式
     *
     * convert Date to cron ,eg. "00 07 10 15 01 ? 2016"
     *
     * @param date
     *            : 时间点
     * @return
     */
    public static String getCron(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("ss mm HH dd MM ? yyyy");
        String formatTimeStr = null;
        if (date != null) {
            formatTimeStr = sdf.format(date);
        }
        return formatTimeStr;
    }
 
    /**
     * 计算两个日期相差几个月
     *
     * @param date1 <String>
     * @param date2 <String>
     * @return int
     * @throws ParseException
     */
    public static int getMonthSpace(String date1, String date2) throws ParseException {
 
        int result = 0;
 
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
 
        c1.setTime(YYYY_MM_DD.parse(date1));
        c2.setTime(YYYY_MM_DD.parse(date2));
 
        result = c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);
 
        return result == 0 ? 1 : Math.abs(result);
 
    }
 
    /**
     * 计算两个日期相差天数
     *
     * @param early <Date>
     * @param late  <Date>
     * @return
     */
    public static final int daysBetween(Date early, Date late) {
        Calendar calst = Calendar.getInstance();
        Calendar caled = Calendar.getInstance();
        calst.setTime(early);
        caled.setTime(late);
        // 设置时间为0时
        calst.set(Calendar.HOUR_OF_DAY, 0);
        calst.set(Calendar.MINUTE, 0);
        calst.set(Calendar.SECOND, 0);
        caled.set(Calendar.HOUR_OF_DAY, 0);
        caled.set(Calendar.MINUTE, 0);
        caled.set(Calendar.SECOND, 0);
        // 得到两个日期相差的天数
        int days = ((int) (caled.getTime().getTime() / 1000) - (int) (calst.getTime().getTime() / 1000)) / 3600 / 24;
        return days;
    }
 
    /**
     * 获取当前时间年度
     * @return
     */
    public static String getNowYear(){
        return String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
    }
 
    /**
     * 计算两个日期相差多少分钟
     *
     * @param early <Date>
     * @param late  <Date>
     * @return
     */
    public static final int minutesBetween(Date early, Date late){
        /*Calendar calst = Calendar.getInstance();
        Calendar caled = Calendar.getInstance();
        calst.setTime(early);
        caled.setTime(late);
        return (int) ((caled.getTime().getTime() - calst.getTime().getTime())/1000/60);*/
        return (daysBetween(early,late)+1)*24*60;
    }
    /**
     * 获取当前日期,日期秒设置为0
     * @return
     */
    public static Date getNewDate(){
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }
 
    /**
     * 计算与当前日期相差给定天数的日期,例如:differ = 1,则返回一天后的日期;differ = -1,则返回昨天的日期
     *
     * @param differ 天数
     * @return
     */
    public static Date calculateDate(int differ) {
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, differ);
        date = calendar.getTime();
        return date;
    }
 
    /**
     * 计算与给定日期相差给定天数的日期,例如:differ = 1,则返回给定日期一天后的日期;differ = -1,则返回给定日期的一天前的日期
     *
     * @param differ 天数
     * @return
     */
    public static Date calculateDate(Date date, int differ) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, differ);
        date = calendar.getTime();
        return date;
    }
 
    /**
     * 日期格式化
     * @param date
     * @return
     */
    public static String getYearByDate(Date date) {
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            int year = calendar.get(Calendar.YEAR);
            return year + "";
        } catch (Exception e) {
            return "未知";
        }
    }
    /**
     * 加一分钟
     */
    public static Date addMinutes(final Date date, int amount) {
        return DateUtils.addMinutes(date, amount);
    }
}