/
DateUtil.java
427 lines (334 loc) · 9.92 KB
/
DateUtil.java
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
package com.github.lazylibrary.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* 日期操作工具类.
*
* @author shimiso
*/
public class DateUtil {
/**
* 英文简写如:2010
*/
public static String FORMAT_Y = "yyyy";
/**
* 英文简写如:12:01
*/
public static String FORMAT_HM = "HH:mm";
/**
* 英文简写如:1-12 12:01
*/
public static String FORMAT_MDHM = "MM-dd HH:mm";
/**
* 英文简写(默认)如:2010-12-01
*/
public static String FORMAT_YMD = "yyyy-MM-dd";
/**
* 英文全称 如:2010-12-01 23:15
*/
public static String FORMAT_YMDHM = "yyyy-MM-dd HH:mm";
/**
* 英文全称 如:2010-12-01 23:15:06
*/
public static String FORMAT_YMDHMS = "yyyy-MM-dd HH:mm:ss";
/**
* 精确到毫秒的完整时间 如:yyyy-MM-dd HH:mm:ss.S
*/
public static String FORMAT_FULL = "yyyy-MM-dd HH:mm:ss.S";
/**
* 精确到毫秒的完整时间 如:yyyy-MM-dd HH:mm:ss.S
*/
public static String FORMAT_FULL_SN = "yyyyMMddHHmmssS";
/**
* 中文简写 如:2010年12月01日
*/
public static String FORMAT_YMD_CN = "yyyy年MM月dd日";
/**
* 中文简写 如:2010年12月01日 12时
*/
public static String FORMAT_YMDH_CN = "yyyy年MM月dd日 HH时";
/**
* 中文简写 如:2010年12月01日 12时12分
*/
public static String FORMAT_YMDHM_CN = "yyyy年MM月dd日 HH时mm分";
/**
* 中文全称 如:2010年12月01日 23时15分06秒
*/
public static String FORMAT_YMDHMS_CN = "yyyy年MM月dd日 HH时mm分ss秒";
/**
* 精确到毫秒的完整中文时间
*/
public static String FORMAT_FULL_CN = "yyyy年MM月dd日 HH时mm分ss秒SSS毫秒";
public static Calendar calendar = null;
private static final String FORMAT = "yyyy-MM-dd HH:mm:ss";
public static Date str2Date(String str) {
return str2Date(str, null);
}
public static Date str2Date(String str, String format) {
if (str == null || str.length() == 0) {
return null;
}
if (format == null || format.length() == 0) {
format = FORMAT;
}
Date date = null;
try {
SimpleDateFormat sdf = new SimpleDateFormat(format);
date = sdf.parse(str);
} catch (Exception e) {
e.printStackTrace();
}
return date;
}
public static Calendar str2Calendar(String str) {
return str2Calendar(str, null);
}
public static Calendar str2Calendar(String str, String format) {
Date date = str2Date(str, format);
if (date == null) {
return null;
}
Calendar c = Calendar.getInstance();
c.setTime(date);
return c;
}
public static String date2Str(Calendar c) {// yyyy-MM-dd HH:mm:ss
return date2Str(c, null);
}
public static String date2Str(Calendar c, String format) {
if (c == null) {
return null;
}
return date2Str(c.getTime(), format);
}
public static String date2Str(Date d) {// yyyy-MM-dd HH:mm:ss
return date2Str(d, null);
}
public static String date2Str(Date d, String format) {// yyyy-MM-dd HH:mm:ss
if (d == null) {
return null;
}
if (format == null || format.length() == 0) {
format = FORMAT;
}
SimpleDateFormat sdf = new SimpleDateFormat(format);
String s = sdf.format(d);
return s;
}
public static String getCurDateStr() {
Calendar c = Calendar.getInstance();
c.setTime(new Date());
return c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) + "-" +
c.get(Calendar.DAY_OF_MONTH) + "-" +
c.get(Calendar.HOUR_OF_DAY) + ":" + c.get(Calendar.MINUTE) +
":" + c.get(Calendar.SECOND);
}
/**
* 获得当前日期的字符串格式
* @param format 格式化的类型
* @return 返回格式化之后的事件
*/
public static String getCurDateStr(String format) {
Calendar c = Calendar.getInstance();
return date2Str(c, format);
}
/**
*
* @param time 当前的时间
* @return 格式到秒
*/
//
public static String getMillon(long time) {
return new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(time);
}
/**
*
* @param time 当前的时间
* @return 当前的天
*/
public static String getDay(long time) {
return new SimpleDateFormat("yyyy-MM-dd").format(time);
}
/**
*
* @param time 时间
* @return 返回一个毫秒
*/
// 格式到毫秒
public static String getSMillon(long time) {
return new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSS").format(time);
}
/**
* 在日期上增加数个整月
* @param date 日期
* @param n 要增加的月数
* @return 增加数个整月
*/
public static Date addMonth(Date date, int n) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH, n);
return cal.getTime();
}
/**
* 在日期上增加天数
* @param date 日期
* @param n 要增加的天数
* @return 增加之后的天数
*/
public static Date addDay(Date date, int n) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, n);
return cal.getTime();
}
/**
* 获取距现在某一小时的时刻
*
* @param format 格式化时间的格式
* @param h 距现在的小时 例如:h=-1为上一个小时,h=1为下一个小时
* @return 获取距现在某一小时的时刻
*/
public static String getNextHour(String format, int h) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date date = new Date();
date.setTime(date.getTime() + h * 60 * 60 * 1000);
return sdf.format(date);
}
/**
* 获取时间戳
* @return 获取时间戳
*/
public static String getTimeString() {
SimpleDateFormat df = new SimpleDateFormat(FORMAT_FULL);
Calendar calendar = Calendar.getInstance();
return df.format(calendar.getTime());
}
/**
* 功能描述:返回月
*
* @param date Date 日期
* @return 返回月份
*/
public static int getMonth(Date date) {
calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.MONTH) + 1;
}
/**
* 功能描述:返回日
*
* @param date Date 日期
* @return 返回日份
*/
public static int getDay(Date date) {
calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.DAY_OF_MONTH);
}
/**
* 功能描述:返回小
*
* @param date 日期
* @return 返回小时
*/
public static int getHour(Date date) {
calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.HOUR_OF_DAY);
}
/**
* 功能描述:返回分
*
* @param date 日期
* @return 返回分钟
*/
public static int getMinute(Date date) {
calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.MINUTE);
}
/**
* 获得默认的 date pattern
* @return 默认的格式
*/
public static String getDatePattern() {
return FORMAT_YMDHMS;
}
/**
* 返回秒钟
*
* @param date Date 日期
* @return 返回秒钟
*/
public static int getSecond(Date date) {
calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.SECOND);
}
/**
* 使用预设格式提取字符串日期
*
* @param strDate 日期字符串
* @return 提取字符串的日期
*/
public static Date parse(String strDate) {
return parse(strDate, getDatePattern());
}
/**
* 功能描述:返回毫
*
* @param date 日期
* @return 返回毫
*/
public static long getMillis(Date date) {
calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.getTimeInMillis();
}
/**
* 按默认格式的字符串距离今天的天数
*
* @param date 日期字符串
* @return 按默认格式的字符串距离今天的天数
*/
public static int countDays(String date) {
long t = Calendar.getInstance().getTime().getTime();
Calendar c = Calendar.getInstance();
c.setTime(parse(date));
long t1 = c.getTime().getTime();
return (int) (t / 1000 - t1 / 1000) / 3600 / 24;
}
/**
* 使用用户格式提取字符串日期
*
* @param strDate 日期字符串
* @param pattern 日期格式
* @return 提取字符串日期
*/
public static Date parse(String strDate, String pattern) {
SimpleDateFormat df = new SimpleDateFormat(pattern);
try {
return df.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
/**
* 按用户格式字符串距离今天的天数
*
* @param date 日期字符串
* @param format 日期格式
* @return 按用户格式字符串距离今天的天数
*/
public static int countDays(String date, String format) {
long t = Calendar.getInstance().getTime().getTime();
Calendar c = Calendar.getInstance();
c.setTime(parse(date, format));
long t1 = c.getTime().getTime();
return (int) (t / 1000 - t1 / 1000) / 3600 / 24;
}
}