-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDateUtils.java
525 lines (474 loc) · 11.6 KB
/
DateUtils.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
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
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
/**
*
* @author kaka2008
*
*/
public class DateUtils {
/**
* 取当前起某段时间间隔后的时间
*
* @param interval
* @return
*/
public static final Calendar getCalendarAfterInterval(long interval) {
Calendar now = Calendar.getInstance();
long time = now.getTimeInMillis() + interval;
now.setTimeInMillis(time);
return now;
}
/**
* 取当前年
*
* @return
*/
public static String getCurrentYear() {
Calendar now = Calendar.getInstance();
return new Integer(now.get(Calendar.YEAR)).toString();
}
/**
* 取当前月
*
* @return
*/
public static String getCurrentMonth() {
Calendar now = Calendar.getInstance();
return new Integer(now.get(Calendar.MONTH) + 1).toString();
}
/**
* 取当前天的字符串
*
* @return
*/
public static String getCurrentDay() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
return format.format(new Date());
}
/**
* 根据某个Date得到字符串,格式为yyyy-MM-dd
*
* @param date
* @return @
*/
public static String getString(Date date) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
return format.format(date);
}
/**
* 根据某个Date得到格式化后的字符串
*
* @param date
* @param formatString
* @return
*/
public static String getString(Date date, String formatString) {
SimpleDateFormat format = new SimpleDateFormat(formatString);
return format.format(date);
}
/**
* 得到某个Date的日期时间字符串,格式为yyyy-MM-dd HH:mm:ss
*
* @param date
* @return
*/
public static String getTimeString(Date date) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return format.format(date);
}
/**
* 得到某个日期的时间部门
*
* @param date
* @return
*/
public static String getTimeStringOnly(Date date) {
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
return format.format(date);
}
/**
* 根据字符串得到某个日期,格式为yyyy-MM-dd
*
* @param dateString
* @return
*/
public static Date getDate(String dateString) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
try {
return format.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 根据字符串得到某个日期,格式为yyyy-MM-dd HH:mm:ss
*
* @param dateString
* @return
*/
public static Date getDateFromTimeString(String dateString) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
return format.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 取某个日期的年份
*
* @param date
* @return
*/
public static String getYear(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return String.valueOf(calendar.get(Calendar.YEAR));
}
/**
* 取某个日期的月份
*
* @param date
* @return
*/
public static String getMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return String.valueOf(calendar.get(Calendar.MONTH) + 1);
}
/**
* 取某个日期的天份
*
* @param date
* @return
*/
public static String getDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return String.valueOf(calendar.get(Calendar.DATE));
}
/**
* 取某一年的第一天
*
* @param year
* @return
*/
public static Date getFirstDayOfYear(String year) {
return getDate(year + "-1-1");
}
/**
* 取某一年的最后一天
*
* @param year
* @return
*/
public static Date getLastDayOfYear(String year) {
return getDate(year + "-12-31");
}
/**
* 取某年某月的第一天
*
* @param month
* @return
*/
public static Date getFirstDayOfMonth(String year, String month) {
Calendar calendar = Calendar.getInstance();
calendar.set(Integer.parseInt(year), Integer.parseInt(month) - 1, 1);
calendar.set(Calendar.DAY_OF_MONTH, calendar
.getActualMinimum(Calendar.DAY_OF_MONTH));
return calendar.getTime();
}
/**
* 取某年某月的最后一天
*
* @param month
* @return
*/
public static Date getLastDayOfMonth(String year, String month) {
Calendar calendar = Calendar.getInstance();
calendar.set(Integer.parseInt(year), Integer.parseInt(month) - 1, 1);
calendar.set(Calendar.DAY_OF_MONTH, calendar
.getActualMaximum(Calendar.DAY_OF_MONTH));
return calendar.getTime();
}
/**
* 取某年某月的第一天
*
* @param month
* @return
*/
public static String getFirstDayOfMonth(Date date) {
try {
Calendar calendar = getCalendarFromDate(date);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
String result = year + "-" + month + "-1";
return result;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
/**
* 取某年某月的最后一天
*
* @param month
* @return
*/
public static String getLastDayOfMonth(Date date) {
try {
Calendar calendar = getCalendarFromDate(date);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int lastDayOfMonth = calendar
.getActualMaximum(Calendar.DAY_OF_MONTH);
String result = year + "-" + month + "-" + lastDayOfMonth;
return result;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// calendar.set(Calendar.DAY_OF_MONTH, calendar
// .getActualMaximum(Calendar.DAY_OF_MONTH));
return "";
}
/**
* 取得某天对应的本周第一天
*
* @param month
* @return
*/
public static String getFirstDateOfWeek(Calendar calendar) {
Calendar result = (Calendar) calendar.clone();
result.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
return getStringFromCalendar(result);
}
/**
* 取得某天对应的本周最后一天
*
* @param month
* @return
*/
static public String getFinalDateOfWeek(Calendar calendar) {
Calendar result = (Calendar) calendar.clone();
result.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
result.add(Calendar.DATE, 8 - result.get(Calendar.DAY_OF_WEEK));
return getStringFromCalendar(result);
}
/**
* 取得某天对应的本周第一天
*
* @param month
* @return
*/
public static String getFirstDateOfWeek(Date date) {
return getFirstDateOfWeek(getCalendarFromDate(date));
}
/**
* 取得某天对应的本周最后一天
*
* @param month
* @return
*/
static public String getFinalDateOfWeek(Date date) {
return getFinalDateOfWeek(getCalendarFromDate(date));
}
/**
* 根据年和第几周 取得周的第一天
*
* @param month
* @return
*/
public static String getFirstDateOfWeek(int year, int week) {
Calendar calendar = Calendar.getInstance(Locale.CHINA);
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.WEEK_OF_YEAR, week);
return getFirstDateOfWeek(calendar);
}
/**
* 根据年和第几周 取得周的最后一天
*
* @param month
* @return
*/
public static String getFinalDateOfWeek(int year, int week) {
Calendar calendar = Calendar.getInstance(Locale.CHINA);
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.WEEK_OF_YEAR, week);
return getFinalDateOfWeek(calendar);
}
/**
* 根据日历得到日期字符串
*
* @param calendar
* @return
*/
public static String getStringFromCalendar(Calendar calendar) {
int currentYear = calendar.get(Calendar.YEAR);
int currentMonth = calendar.get(Calendar.MONTH) + 1;
int currentDay = calendar.get(Calendar.DATE);
return String.valueOf(currentYear) + "-" + String.valueOf(currentMonth)
+ "-" + String.valueOf(currentDay);
}
/**
* 根据Date 得到对应的Calendar
*
* @param date
* @return @
*/
public static Calendar getCalendarFromDate(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
return c;
}
/**
* 判断两个日期是否是同一天
*
* @param date1
* @param date2
* @return
*/
public static boolean isSameDay(Date date1, Date date2) {
return org.apache.commons.lang.time.DateUtils.isSameDay(date1, date2);
}
/**
* 得到下一年
*
* @param year
* @return
*/
public static String getNextYear(String year) {
if (year == null || "".equals(year))
return "";
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, Integer.parseInt(year));
c.add(Calendar.YEAR, 1);
int nextYear = c.get(Calendar.YEAR);
return String.valueOf(nextYear);
}
/**
* 得到上一年
*
* @param year
* @return
*/
public static String getPrevYear(String year) {
if (year == null || "".equals(year))
return "";
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, Integer.parseInt(year));
c.add(Calendar.YEAR, -1);
int nextYear = c.get(Calendar.YEAR);
return String.valueOf(nextYear);
}
/**
* 取前或者后多少年
*
* @param year
* @param num
* @return
*/
public static String getManyBackYear(String year, Integer num) {
if (year == null || "".equals(year))
return "";
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, Integer.parseInt(year));
c.add(Calendar.YEAR, num);
int nextYear = c.get(Calendar.YEAR);
return String.valueOf(nextYear);
}
/**
* 得到某一天的开始时间:即yyyy-mm-dd 00:00:00 000
*
* @param date
* @return
*/
public static Date getStartOfDay(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.AM_PM, Calendar.AM);
c.set(Calendar.HOUR, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 000);
return c.getTime();
}
/**
* 得到某一天的结束时间:即yyyy-mm-dd 11:59:59 999
*
* @param date
* @return
*/
public static Date getEndOfDay(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.AM_PM, Calendar.PM);
c.set(Calendar.HOUR, 11);
c.set(Calendar.MINUTE, 59);
c.set(Calendar.SECOND, 59);
c.set(Calendar.MILLISECOND, 999);
return c.getTime();
}
/**
* 得到某天的下一天
*
* @param date
* @return
*/
public static Date getNextDay(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.DATE, 1);
return c.getTime();
}
/**
* 根据字符串格式 得到指定格式的字符串
*
* @author tantao
* @param date
* @param formatString
* 指定格式的字符串例如[yyyy-MM-dd]
* @return
*/
public static String getDateStringByFormatString(Date date,
String formatString) {
try {
if (formatString == null)
formatString = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(formatString);
return sdf.format(date);
} catch (Exception e) {
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_STRING_COMMON_CN);
return sdf.format(new Date());
}
}
public static String DATE_FORMAT_STRING_COMMON="yyyy-MM-dd";
public static String DATE_FORMAT_STRING_DEFAULT="yyyy-MM-dd HH:mm:ss";
public static String DATE_FORMAT_STRING_DEFAULT_CN="yyyy年MM月dd日 HH时mm分ss秒";
public static String DATE_FORMAT_STRING_COMMON_CN="yyyy年MM月dd日";
/**
* 两天的间隔数
*
* @author tantao
* @param begin
* Date 开始日期
* @param end
* Date 结束日期
*
* @return 两天的间隔数
*/
public static Long getIntervalDate(Date begin, Date end) {
if (begin == null || end == null)
return 0L;
Long num = begin.getTime() - end.getTime();
num = num / 1000 / 60 / 60 / 24;
return num;
}
}