#
DELL
2025-09-18 3d10ceed030be39cc520aa453e36e695b1638b81
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
package com.zy.common.utils;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
 
public class DateUtils {
    public static final String yyyyMMdd_C = "yyyy年MM月dd日";
    public static final String yyyyMM_F = "yyyy-MM";
    public static final String yyyyMMdd_F = "yyyy-MM-dd";
    public static final String yyyyMMddHHmmss_F = "yyyy-MM-dd HH:mm:ss";
    public static final String yyyyMMddHHmmsssss_F = "yyyy-MM-dd HH:mm:ss,SSS";
    public static final String yyyy = "yyyy";
    public static final String yyyyMM = "yyyyMM";
    public static final String yyyyMMdd = "yyyyMMdd";
    public static final String yyyyMMddHH = "yyyyMMddHH";
    public static final String yyyyMMddHHmmss = "yyyyMMddHHmmss";
    public static final String YYMMDDHHMMSS = "YYMMDDHHMMSS";
    public static final String yyyyMMddHHmmsssss = "yyyyMMddHHmmssSSS";
 
    public static String convert(Date date, String pattern) {
        return (new SimpleDateFormat(pattern)).format(date);
    }
 
    public static String convert(Date date) {
        return convert(date, "yyyy-MM-dd HH:mm:ss");
    }
 
    public static Date convert(String str, String pattern) {
        if (str.length() < pattern.length()) {
            throw new RuntimeException("时间解析失败 ==>> " + str);
        } else {
            if (str.length() > pattern.length()) {
                str = str.substring(0, pattern.length());
            }
 
            SimpleDateFormat format = new SimpleDateFormat(pattern);
 
            try {
                Date date = format.parse(str);
                return date;
            } catch (ParseException var5) {
                throw new RuntimeException("时间解析失败 ==>> " + str);
            }
        }
    }
 
    public static Date convert(String str) {
        return convert(str, "yyyy-MM-dd HH:mm:ss");
    }
 
    public static int diff(Date date1, Date date2) {
        return getDaysByTimestamp(Math.abs(date2.getTime() - date1.getTime()));
    }
 
    public static long diffToMinute(Date date1, Date date2) {
        return Math.abs(date2.getTime() - date1.getTime()) / 1000L / 60L;
    }
 
    public static long diffToSeconds(Date date1, Date date2) {
        return Math.abs(date2.getTime() - date1.getTime()) / 1000L;
    }
 
    private static int getDaysByTimestamp(long timestamp) {
        double daysPoint = Arith.divides(2, new Number[]{timestamp, 86400000});
        int daysPoint1 = (int)daysPoint;
        double daysPoint2 = (double)daysPoint1;
        return daysPoint > daysPoint2 ? daysPoint1 + 1 : daysPoint1;
    }
 
    public static int diffToNow(Date date) {
        long diff = (new Date()).getTime() - date.getTime();
        return (int)(Math.abs(diff) / 1000L);
    }
 
    public static String createTimeStamp() {
        return Long.toString(System.currentTimeMillis() / 1000L);
    }
 
    public static Date calculate(Date date, Long val, TimeUnit timeUnit, boolean subtraction) {
        return !Objects.isNull(date) && !Objects.isNull(val) && !Objects.isNull(timeUnit) ? new Date(subtraction ? date.getTime() - timeUnit.toMillis(val) : date.getTime() + timeUnit.toMillis(val)) : null;
    }
 
    public static Date calculate(Date date, Long val, TimeUnit timeUnit) {
        return calculate(date, val, timeUnit, false);
    }
 
    public static DateEntity getDateEntity(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        DateEntity dateEntity = new DateEntity();
        dateEntity.setYear(cal.get(1));
        dateEntity.setMonth(cal.get(2));
        dateEntity.setDay(cal.get(5));
        dateEntity.setHour(cal.get(11));
        dateEntity.setMinute(cal.get(12));
        dateEntity.setSecond(cal.get(13));
        return dateEntity;
    }
 
    static class DateEntity {
        int year;
        int month;
        int day;
        int hour;
        int minute;
        int second;
 
        public int getYear() {
            return this.year;
        }
 
        public void setYear(int year) {
            this.year = year;
        }
 
        public int getMonth() {
            return this.month;
        }
 
        public void setMonth(int month) {
            this.month = month + 1;
        }
 
        public int getDay() {
            return this.day;
        }
 
        public void setDay(int day) {
            this.day = day;
        }
 
        public int getHour() {
            return this.hour;
        }
 
        public void setHour(int hour) {
            this.hour = hour;
        }
 
        public int getMinute() {
            return this.minute;
        }
 
        public void setMinute(int minute) {
            this.minute = minute;
        }
 
        public int getSecond() {
            return this.second;
        }
 
        public void setSecond(int second) {
            this.second = second;
        }
    }
}