1
zj
2024-08-12 11bb86a81c99672e5e51ca7289f49a57346739e8
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
package util;
 
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
 
import org.apache.commons.lang.StringUtils;
import org.springframework.util.Assert;
 
public abstract class DateUtils {
 
    public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
 
    public static final String DEFAULT_TIME_ZONE = "GMT-4:00";
 
    public static final String NORMAL_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
 
    /**
     * 根据yyyy-MM-dd HH:mm:ss格式返回指定时间
     * 
     * @return
     */
    public static final String formatOfDateTime(Date date) {
        return format(date, NORMAL_DATE_FORMAT);
    }
 
    /**
     * 根据yyyy-MM-dd HH:mm:ss格式返回当前时间
     * 
     * @return
     */
    public static final String formatOfDateTime() {
        return formatOfDateTime(new Date());
    }
 
    public static final String format(Date date, String pattern) {
        if (date == null) {
            throw new NullPointerException("时间不能为NULL!");
        }
 
        if (StringUtils.isEmpty(pattern)) {
            throw new IllegalArgumentException("格式pattern不能为空字符串!");
        }
 
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
 
        DateFormat formater = new SimpleDateFormat(pattern);
        formater.setCalendar(calendar);
 
        return formater.format(date);
    }
 
    public static Date toDate(String string) {
        return toDate(string, DEFAULT_DATE_FORMAT);
    }
 
    public static Date toDate(String string, String pattern) {
        return toDate(string, pattern, TimeZone.getTimeZone(DEFAULT_TIME_ZONE));
    }
 
    public static Date toDate(String string, String pattern, TimeZone timeZone) {
        try {
            SimpleDateFormat sdf = (SimpleDateFormat) createDateFormat(pattern, timeZone);
            return sdf.parse(string);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
 
    public static String format(Date date, String pattern, TimeZone timeZone) {
        DateFormat df = createDateFormat(pattern, timeZone);
        return df.format(date);
    }
 
    public static DateFormat createDateFormat(String pattern) {
        return createDateFormat(pattern, TimeZone.getTimeZone(DEFAULT_TIME_ZONE));
    }
 
    public static DateFormat createDateFormat(String pattern, TimeZone timeZone) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        TimeZone gmt = timeZone;
        sdf.setTimeZone(gmt);
        sdf.setLenient(true);
        return sdf;
    }
 
    public static int getYear(java.util.Date date) {
        Calendar calendar = (Calendar) Calendar.getInstance().clone();
        calendar.setTime(date);
        return calendar.get(Calendar.YEAR);
    }
 
    public static String getShortYear(java.util.Date date) {
        String year = getYear(date) + "";
        int length = year.length();
        return year.substring(length - 2, length);
    }
 
    public static int getMonth(java.util.Date date) {
        Calendar calendar = (Calendar) Calendar.getInstance().clone();
        calendar.setTime(date);
        return calendar.get(Calendar.MONTH) + 1;
    }
 
    public static int getDay(java.util.Date date) {
        Calendar calendar = (Calendar) Calendar.getInstance().clone();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_MONTH);
    }
 
    public static int getHour(java.util.Date date) {
        Calendar calendar = (Calendar) Calendar.getInstance().clone();
        calendar.setTime(date);
        return calendar.get(Calendar.HOUR_OF_DAY);
    }
 
    public static int getMinute(java.util.Date date) {
        Calendar calendar = (Calendar) Calendar.getInstance().clone();
        calendar.setTime(date);
        return calendar.get(Calendar.MINUTE);
    }
 
    public static int getSecond(java.util.Date date) {
        Calendar calendar = (Calendar) Calendar.getInstance().clone();
        calendar.setTime(date);
        return calendar.get(Calendar.SECOND);
    }
 
    public static Date addMilliSecond(java.util.Date oldDate, int addMilliSecond) {
        Calendar calendar = (Calendar) Calendar.getInstance().clone();
        calendar.setTime(oldDate);
        calendar.add(Calendar.MILLISECOND, addMilliSecond);
        return calendar.getTime();
    }
 
    public static Date addSecond(java.util.Date oldDate, int addSecond) {
        Calendar calendar = (Calendar) Calendar.getInstance().clone();
        calendar.setTime(oldDate);
        calendar.add(Calendar.SECOND, addSecond);
        return calendar.getTime();
    }
 
    public static Date addMinute(java.util.Date oldDate, int addMinutes) {
        Calendar calendar = (Calendar) Calendar.getInstance().clone();
        calendar.setTime(oldDate);
        calendar.add(Calendar.MINUTE, addMinutes);
        return calendar.getTime();
    }
 
    public static Date addHour(java.util.Date oldDate, int addHours) {
        Calendar calendar = (Calendar) Calendar.getInstance().clone();
        calendar.setTime(oldDate);
        calendar.add(Calendar.HOUR, addHours);
        return calendar.getTime();
    }
 
    public static Date addDay(java.util.Date oldDate, int addDays) {
        Calendar calendar = (Calendar) Calendar.getInstance().clone();
        calendar.setTime(oldDate);
        calendar.add(Calendar.DATE, addDays);
        return calendar.getTime();
    }
 
    public static Date addMonth(java.util.Date oldDate, int addMonths) {
        Calendar calendar = (Calendar) Calendar.getInstance().clone();
        calendar.setTime(oldDate);
        calendar.add(Calendar.MONTH, addMonths);
        return calendar.getTime();
    }
 
    public static Date addYear(java.util.Date oldDate, int addYears) {
        Calendar calendar = (Calendar) Calendar.getInstance().clone();
        calendar.setTime(oldDate);
        calendar.add(Calendar.YEAR, addYears);
        return calendar.getTime();
    }
 
    public static long calcTimeBetween(String unitType, Date startDate, Date endDate) {
        Assert.hasText(unitType);
        Assert.notNull(startDate);
        Assert.notNull(endDate);
        long between = endDate.getTime() - startDate.getTime();
        if (unitType.equals("ms")) {
            return between;
        } else if (unitType.equals("s")) {
            return between / 1000;// 返回秒
        } else if (unitType.equals("m")) {
            return between / 60000;// 返回分钟
        } else if (unitType.equals("h")) {
            return between / 3600000;// 返回小时
        } else if (unitType.equals("d")) {
            return between / 86400000;// 返回天数
        } else {
            throw new IllegalArgumentException("the unitType is unknown");
        }
    }
 
    public static long calcTimeBetweenInMillis(Date startDate, Date endDate) {
        return calcTimeBetween("ms", startDate, endDate);
    }
 
    public static long calcTimeBetweenInSecond(Date startDate, Date endDate) {
        return calcTimeBetween("s", startDate, endDate);
    }
 
    public static long calcTimeBetweenInMinute(Date startDate, Date endDate) {
        return calcTimeBetween("m", startDate, endDate);
    }
 
    public static long calcTimeBetweenInHour(Date startDate, Date endDate) {
        return calcTimeBetween("h", startDate, endDate);
    }
 
    public static long calcTimeBetweenInDay(Date startDate, Date endDate) {
        return calcTimeBetween("d", startDate, endDate);
    }
 
    public static Date roundYear(Date date) {
        return org.apache.commons.lang.time.DateUtils.round(date, Calendar.YEAR);
    }
 
    public static Date roundMonth(Date date) {
        return org.apache.commons.lang.time.DateUtils.round(date, Calendar.MONTH);
    }
 
    public static Date roundDay(Date date) {
        return org.apache.commons.lang.time.DateUtils.round(date, Calendar.DATE);
    }
 
    public static Date roundHour(Date date) {
        return org.apache.commons.lang.time.DateUtils.round(date, Calendar.HOUR);
    }
 
    public static Date roundMinute(Date date) {
        return org.apache.commons.lang.time.DateUtils.round(date, Calendar.MINUTE);
    }
 
    public static Date roundSecond(Date date) {
        return org.apache.commons.lang.time.DateUtils.round(date, Calendar.SECOND);
    }
 
    public static Date truncateYear(Date date) {
        return org.apache.commons.lang.time.DateUtils.truncate(date, Calendar.YEAR);
    }
 
    public static Date truncateMonth(Date date) {
        return org.apache.commons.lang.time.DateUtils.truncate(date, Calendar.MONTH);
    }
 
    public static Date truncateDay(Date date) {
        return org.apache.commons.lang.time.DateUtils.truncate(date, Calendar.DATE);
    }
 
    public static Date truncateHour(Date date) {
        return org.apache.commons.lang.time.DateUtils.truncate(date, Calendar.HOUR);
 
    }
 
    public static Date truncateMinute(Date date) {
        return org.apache.commons.lang.time.DateUtils.truncate(date, Calendar.MINUTE);
    }
 
    public static Date truncateSecond(Date date) {
        return org.apache.commons.lang.time.DateUtils.truncate(date, Calendar.SECOND);
    }
 
    public static Date setHour(Date oldDate, int newHour) {
        Calendar calendar = (Calendar) Calendar.getInstance().clone();
        calendar.setTime(oldDate);
        calendar.set(Calendar.HOUR, newHour);
        return calendar.getTime();
    }
 
    public static Date setMinute(Date oldDate, int newMinute) {
        Calendar calendar = (Calendar) Calendar.getInstance().clone();
        calendar.setTime(oldDate);
        calendar.set(Calendar.MINUTE, newMinute);
        return calendar.getTime();
    }
 
    public static Date setSecond(Date oldDate, int newSecond) {
        Calendar calendar = (Calendar) Calendar.getInstance().clone();
        calendar.setTime(oldDate);
        calendar.set(Calendar.SECOND, newSecond);
        return calendar.getTime();
    }
 
    /**
     * 
     * @param dt Date
     * @return boolean
     */
    @SuppressWarnings("deprecation")
    public static boolean isRYear(Date dt) {
        return (isRYear(1900 + dt.getYear()));
    }
 
    /**
     * 
     * @param y int
     * @return boolean
     */
    public static boolean isRYear(int y) {
        return (y % 400 == 0 || (y % 4 == 0 && y % 100 != 0));
    }
 
    /**
     * 获取一个日期的时间字符串
     * 
     * @param dt Date
     * @return String
     */
    public static String getTimeStr(Date dt) {
        return new SimpleDateFormat("HH:mm:ss").format(dt);
    }
 
    /**
     * 获取一个日期值的日期字符串
     * 
     * @param dt Date
     * @return String
     */
    public static String getDateStr(Date dt) {
        return new SimpleDateFormat("yyyy-MM-dd").format(dt);
    }
 
    /**
     * 获取一个日期值的带时间日期字符串
     * 
     * @param dt Date
     * @return String
     */
    public static String getLongDate(Date dt) {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(dt);
    }
 
    /**
     * 
     * @param dt Date
     * @return String
     */
    public static String toString(Date dt) {
        return format(dt, "yyyy-MM-dd HH:mm:ss");
    }
 
    /**
     * 
     * @param date Date
     * @return Timestamp added by jiayc
     */
    public static java.sql.Timestamp dateToTimeStamp(java.util.Date date) {
        if (date == null) {
            return null;
        } else {
            return new java.sql.Timestamp(date.getTime());
        }
    }
 
}