无关风月
2024-12-09 2053b8fe0e98d4b4449bc756a93ced78f42277c4
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
package com.jilongda.common.utils;
 
import com.jilongda.common.basic.Constant;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.*;
 
/**
 * @author xiaochen
 * @ClassName DateUtils
 * @Description
 * @date 2021-05-25 11:02
 */
public class TimeUtils {
    /**
     * 时间格式(yyyy-MM-dd)
     */
    public final static String DATE_PATTERN = "yyyy-MM-dd";
    /**
     * 时间格式(yyyy-MM-dd HH:mm:ss)
     */
    public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
 
    /**
     * @param
     * @return java.lang.String
     * @author FEIYC
     * @date 2021/6/25 14:56
     * @description 获取流水号日期
     */
    public static String getStreamCodeData() {
        SimpleDateFormat dateStreamCodeFormat = new SimpleDateFormat(Constant.DATESTREAMCODE);
        return dateStreamCodeFormat.format(new Date());
    }
 
 
    /**
     * @param
     * @return java.lang.String
     * @author feiyunchuan
     * @date 2021/7/9 17:21
     * @description 获取今日日期
     */
    public static String getTodayDate() {
        SimpleDateFormat dateStreamCodeFormat = new SimpleDateFormat(Constant.DATE_FORMATTER_DATE);
        return dateStreamCodeFormat.format(new Date());
    }
 
    /**
     * @param
     * @return java.lang.String
     * @author FEIYC
     * @date 2021/6/25 14:56
     * @description 获取流水号时间
     */
    public static String getStreamCodeDataTime() {
        SimpleDateFormat dateStreamCodeFormat = new SimpleDateFormat(Constant.DATETIMESTREAMCODE);
        return dateStreamCodeFormat.format(new Date());
    }
 
    /**
     * 仓库创建时间
     *
     * @return
     */
    public static String getCreateTime() {
        SimpleDateFormat dateFormat = new SimpleDateFormat(Constant.DATE_FORMATTER_TIME);
        return dateFormat.format(new Date());
    }
 
    /**
     * 获取两个日期之间所有的天数集合
     *
     * @param startTime
     * @param endTime
     * @return:YYYY-MM
     */
    public static List<String> getDayList(String startTime, String endTime, String formatStr) {
        SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
        // 声明保存日期集合
        List<String> list = new ArrayList<String>();
        try {
            // 转化成日期类型
            Date startDate = sdf.parse(startTime);
            Date endDate = sdf.parse(endTime);
 
            //用Calendar 进行日期比较判断
            Calendar calendar = Calendar.getInstance();
            while (startDate.getTime() <= endDate.getTime()) {
                // 把日期添加到集合
                list.add(sdf.format(startDate));
                // 设置日期
                calendar.setTime(startDate);
                //把日期增加一天
                calendar.add(Calendar.DATE, 1);
                // 获取增加后的日期
                startDate = calendar.getTime();
            }
        } catch (ParseException e) {
            throw new RuntimeException("日期格式转换错误");
        }
        return list;
    }
 
    /**
     * 获取日期的24小时制
     *
     * @param date
     * @return:YYYY-MM
     */
    public static List<String> getDayHourList(Date date, String resultFormatStr) {
        SimpleDateFormat sdf = new SimpleDateFormat(Constant.DATE_FORMATTER_DATE);
        // 声明保存日期集合
        List<String> list = new ArrayList<>(24);
        try {
            String dateStr;
            Date d;
            SimpleDateFormat sdfd = new SimpleDateFormat(Constant.DATE_FORMATTER_DATE_HOUR);
            SimpleDateFormat resSdf = new SimpleDateFormat(resultFormatStr);
            for (int i = 0; i <= 23; i++) {
                if (String.valueOf(i).length() == 0) {
                    dateStr = sdf.format(date) + " 0" + i;
                } else {
                    dateStr = sdf.format(date) + " " + i;
                }
                d = sdfd.parse(dateStr);
                list.add(resSdf.format(d));
            }
        } catch (ParseException e) {
            throw new RuntimeException("日期格式转换错误");
        }
        return list;
    }
 
 
    /***
     * 获取日期的24小时制
     * @author xiaochen
     * @date 2021-12-30 19:51
     * @return java.util.List<java.lang.String>
     */
    public static List<String> getDayHourList() {
        List<String> times = new ArrayList<>(24);
        for (int i = 0; i <= 23; i++) {
            if (i < 10) {
                times.add("0" + i + ":00");
            } else {
                times.add(i + ":00");
            }
        }
        return times;
    }
 
    /**
     * 将时间戳字符串转为Date类型
     *
     * @param timestamp 时间戳
     * @return Date日期
     * @throws ParseException 格式化异常
     */
    public static Date getDateByTimestamp(String timestamp, String patternStr) {
        SimpleDateFormat format = new SimpleDateFormat(patternStr);
        Long times = Long.valueOf(timestamp);
        String d = format.format(times);
        try {
            return format.parse(d);
        } catch (ParseException e) {
            throw new RuntimeException("字符串时间转换错误");
        }
    }
 
    /**
     * 将时间戳字符串转为Date类型
     *
     * @param time 时间
     * @return Date日期
     * @throws ParseException 格式化异常
     */
    public static Date getDate(Date time, String patternStr) {
        SimpleDateFormat format = new SimpleDateFormat(patternStr);
        String d = format.format(time);
        try {
            return format.parse(d);
        } catch (ParseException e) {
            throw new RuntimeException("字符串时间转换错误");
        }
    }
 
    /**
     * 指定日期所在周的周一和周日时间
     *
     * @return 结果集
     */
    public static Map<String, Date> getWeekDate(Date date) {
        Map<String, Date> map = new HashMap<>(2);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if (dayWeek == 1) {
            dayWeek = 8;
        }
        // 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - dayWeek);
        Date mondayDate = cal.getTime();
        cal.add(Calendar.DATE, 4 + cal.getFirstDayOfWeek());
        Date sundayDate = cal.getTime();
        map.put("first", mondayDate);
        map.put("last", sundayDate);
        return map;
    }
 
    /**
     * 指定日期所在月的第一天/最后一天时间
     *
     * @return 结果集
     */
    public static Map<String, Date> getMonthDate(Date date) {
        Map<String, Date> map = new HashMap<>(2);
        Calendar cal = Calendar.getInstance();
        //设置指定日期
        cal.setTime(date);
        //获取当月第一天日期
        int first = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, first);
        Date firstDay = cal.getTime();
        map.put("first", firstDay);
        //获取当月最后一天日期
        int last = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, last);
        Date lastDay = cal.getTime();
        map.put("last", lastDay);
        return map;
    }
 
    /**
     * 获取日期逻辑中所在的季度数
     *
     * @param date 当前日期
     * @return 第几季度
     */
    public static int getQuarterOfYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MONTH) / 3 + 1;
    }
 
    /**
     * 指定日期所在季度的第一天/最后一天时间
     *
     * @return 结果集
     */
    public static Map<String, Date> getQuarterDate(Date date) {
        Map<String, Date> map = new HashMap<>(2);
 
        int quarter = getQuarterOfYear(date);
        int year = dateToLocalDateTime(date).getYear();
 
        int startMonth = (quarter - 1) * 3;
        // 根据月获取开始时间
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, startMonth);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        Date first = cal.getTime();
        map.put("first", first);
 
        int lastMonth = quarter * 3 - 1;
        // 根据月获取结束时间
        cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, lastMonth);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        Date last = cal.getTime();
        map.put("last", last);
 
        return map;
    }
 
    /**
     * 指定日期所在半年的第一天/最后一天时间
     *
     * @return 结果集
     */
    public static Map<String, LocalDateTime> getHalfYearDate(Date date) {
        Map<String, LocalDateTime> map = new HashMap<>(2);
 
        int quarter = getQuarterOfYear(date);
        int year = dateToLocalDateTime(date).getYear();
        if(quarter == 1 || quarter == 2){
            map.put("first", LocalDateTime.of(year, 1, 1, 0, 0, 0));
            map.put("last", LocalDateTime.of(year, 6, 30, 23, 59, 59));
        }else {
            map.put("first", LocalDateTime.of(year, 7, 1, 0, 0, 0));
            map.put("last", LocalDateTime.of(year, 12, 31, 23, 59, 59));
        }
        return map;
    }
 
    /**
     * 指定日期所在年的第一天/最后一天时间
     *
     * @return 结果集
     */
    public static Map<String, Date> getYearDate(Date date) {
        Map<String, Date> map = new HashMap<>(2);
        Calendar cal = Calendar.getInstance();
        //设置指定日期
        cal.setTime(date);
        //获取本年第一天日期
        int first = cal.getActualMinimum(Calendar.DAY_OF_YEAR);
        cal.set(Calendar.DAY_OF_YEAR, first);
        Date firstDay = cal.getTime();
        map.put("first", firstDay);
        //获取本年最后一天日期
        int last = cal.getActualMaximum(Calendar.DAY_OF_YEAR);
        cal.set(Calendar.DAY_OF_YEAR, last);
        Date lastDay = cal.getTime();
        map.put("last", lastDay);
        return map;
    }
 
    /**
     * 分别获取日期中的年月日
     *
     * @param date 需要获取的日期
     * @return 结果集合
     */
    public static Map<String, String> getYearMonthDay(Date date) {
        String year = String.format("%tY", date);
        String month = String.format("%tm", date);
        String day = String.format("%td", date);
        Map<String, String> result = new HashMap<>(3);
        result.put("year", year);
        result.put("month", month);
        result.put("day", day);
        result.put("time", String.valueOf(date.getTime()));
        return result;
    }
 
    /**
     * 获取指定日期字符串的LocalDateTime
     * String转LocalDateTime
     *
     * @param time 日期字符串
     * @return 结果LocalDateTime
     */
    public static LocalDateTime getLocalDateTime(String time) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        LocalDate localDate = LocalDate.parse(time, dateTimeFormatter);
        Date date = Date.from(localDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant());
        return date.toInstant().atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
    }
 
 
    /**
     * 将时间戳字符串转为LocalDateTime类型
     *
     * @param time 时间戳
     * @return LocalDateTime
     * @throws ParseException 格式化异常
     */
    public static LocalDateTime getLocalDateTimeByTimeStamp(String time) {
        LocalDateTime localDateTime = Instant.ofEpochMilli(Long.parseLong(time)).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
        return localDateTime;
    }
 
 
    /**
     * Date转为LocalDateTime
     *
     * @param date 日期
     * @return LocalDateTime
     */
    public static LocalDateTime dateToLocalDateTime(Date date) {
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        return instant.atZone(zoneId).toLocalDateTime();
    }
 
    /**
     * LocalDateTime转Date
     *
     * @param dateTime 日期
     * @return Date
     */
    public static Date localDateTimeToDate(LocalDateTime dateTime) {
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = dateTime.atZone(zoneId);
        return Date.from(zdt.toInstant());
    }
 
    /**
     * 获取当前时间到凌晨0点的秒数
     *
     * @return 当前时间到凌晨0点的秒数
     */
    public static long getSecondsFromNowToAm() throws ParseException {
        long now = System.currentTimeMillis();
        SimpleDateFormat sdfOne = new SimpleDateFormat("yyyy-MM-dd");
        return (now - (sdfOne.parse(sdfOne.format(now)).getTime())) / 1000;
    }
 
    /**
     * 根据身份证获取年龄
     *
     * @param idCard 身份证件号
     * @return 年龄
     */
    public static int getAge(String idCard) {
        int year = Integer.parseInt(idCard.substring(6, 10));
        int date = Integer.parseInt(idCard.substring(10, 14));
        Map<String, String> yearMonthDay = getYearMonthDay(new Date());
        int currYear = Integer.parseInt(yearMonthDay.get("year"));
        int currDate = Integer.parseInt(yearMonthDay.get("month") + yearMonthDay.get("day"));
        int age = currYear - year;
        if (currDate < date) {
            age -= 1;
        }
        return age;
    }
 
 
    /**
     * 根据身份证获取年龄
     *
     * @param idCard 身份证件号
     * @return 年龄
     */
    public static int getSex(String idCard) {
        int sex = Integer.parseInt(idCard.substring(16, 17));
        if (sex % 2 == 1) {
            sex = 1;
        } else {
            sex = 2;
        }
        return sex;
    }
 
    /**
     * 根据身份证获取生日
     *
     * @param idCard 身份证号码
     * @return 生日
     */
    public static LocalDateTime getBirthday(String idCard) {
        String year = idCard.substring(6, 10);
        String month = idCard.substring(10, 12);
        String day = idCard.substring(12, 14);
        String birthday = year + Constant.SEP_LINE + month + Constant.SEP_LINE + day + " 00:00:00";
        return getLocalDateTime(birthday);
    }
 
    /**
     * 计算两个日期之间相差的天数
     *
     * @param smdate 较小的时间
     * @param bdate  较大的时间
     * @return 相差天数
     */
    public static int daysBetween(Date smdate, Date bdate) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        long days = (time2 - time1) / (1000 * 3600 * 24);
        return Integer.parseInt(String.valueOf(days));
    }
 
    /**
     * localdatetime转为字符串
     *
     * @param time localdatetime
     * @return 字符串
     */
    public static String localDateTimeToString(LocalDateTime time) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return df.format(time);
    }
 
    /**
     * localdate转为字符串
     *
     * @param time localdate
     * @return 字符串
     */
    public static String localDateToString(LocalDate time) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return df.format(time);
    }
 
    /**
     * localdatetime转为年月日字符串
     *
     * @param time localdatetime
     * @return 字符串
     */
    public static String localDateTimeToYMDString(LocalDateTime time) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return df.format(time);
    }
 
    /**
     * 获取当天的00:00:00
     *
     * @return
     */
    public static LocalDateTime getDayStart(LocalDateTime time) {
        return time.with(LocalTime.MIN);
    }
 
 
    /**
     * 获取当天的23:59:59
     *
     * @return
     */
    public static LocalDateTime getDayEnd(LocalDateTime time) {
        return time.with(LocalTime.MAX);
    }
 
    /**
     * 获取指定日期的当月第一天的0点
     *
     * @return
     */
    public static String getDayStartString(Date now) {
        Date date = getMonthDate(now).get("first");
        LocalDateTime localDateTime = dateToLocalDateTime(date);
        LocalDateTime dayStart = getDayStart(localDateTime);
        String start = localDateTimeToString(dayStart);
        return start;
    }
 
    /**
     * 获取指定日期的上月第一天的0点
     *
     * @return
     */
    public static String getLastStartString(Date now) {
        Date date = getMonthDate(now).get("first");
        LocalDateTime localDateTime = dateToLocalDateTime(date);
        LocalDateTime LstEnd = getDayStart(localDateTime).plusMonths(-1L);
        return localDateTimeToString(LstEnd);
    }
 
 
    /**
     * 计算两个时间的时间差(时:分)
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static String getTodayLocalTimeDiff(LocalTime startTime, LocalTime endTime) {
        try {
            // 获取结束时间的小时与分钟
            int endTimeHour = endTime.getHour();
            int endTimeMinute = endTime.getMinute();
            // 获取开始时间的小时与分钟
            int startTimeHour = startTime.getHour();
            int startTimeMinute = startTime.getMinute();
            // 计算时间差
            int hour = Math.abs(endTimeHour - startTimeHour);
            int minute = Math.abs(endTimeMinute - startTimeMinute);
            if (endTimeMinute < startTimeMinute) {
                hour = hour - 1;
                minute = 60 - minute;
            }
            return hour + ":" + minute;
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return null;
    }
 
    /**
     * 时间格式(时:分)转分钟
     *
     * @return
     */
    public static int getHourToMinute(String time) {
        try {
            // 获取结束时间的小时与分钟
            String[] split = time.split(Constant.SEP_COLON);
            String hour = split[0];
            String minute = split[1];
            // 计算时间差
            int minutes = Integer.parseInt(hour) * 60;
            return minutes + Integer.parseInt(minute);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return 0;
    }
}