44323
2023-12-28 c31f9914a85889bbe090d907ed53cc7a2fdcf8b6
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
package com.dsh.guns.modular.system.util;
 
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.*;
 
public class TimeUtil {
 
    private static final String DATE_FORMATTER_TIME = "yyyy-MM-dd HH:mm:ss";
 
    /**
     * 获取当天的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);
    }
 
    /**
     * 获取指定日期字符串的LocalDateTime
     * String转LocalDateTime
     *
     * @param time 日期字符串
     * @return 结果LocalDateTime
     */
    public static LocalDateTime getLocalDateTime(String time) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd", 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();
    }
 
    /**
     * 指定日期所在月的第一天/最后一天时间
     *
     * @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;
    }
 
    /**
     * 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();
    }
 
}