-
Notifications
You must be signed in to change notification settings - Fork 2
/
CalendarTools.java
executable file
·575 lines (519 loc) · 12.9 KB
/
CalendarTools.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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
/**
*
* 说明:时间日期处理类
*
* @author zheng_zhi_rui@163com
* @date 2015年3月25日
*
*/
public final class CalendarTools {
public static final String DATE = "yyyy-MM-dd";
public static final String TIME = "HH:mm:ss";
public static final String DATETIME = "yyyy-MM-dd HH:mm:ss";
public static Calendar getCalendar(Date date) {
if (date == null) {
return null;
}
Calendar calendar = GregorianCalendar.getInstance();
calendar.setTime(date);
return calendar;
}
public static Calendar getCalendar(int year, int month, int day, int hour, int minute, int second) {
Calendar calendar = GregorianCalendar.getInstance();
calendar.set(year, month, day, hour, minute, second);
return calendar;
}
public static Calendar getCalendar(long millis) {
Calendar calendar = GregorianCalendar.getInstance();
calendar.setTimeInMillis(millis);
return calendar;
}
public static Date getDate(Calendar calendar) {
if (calendar == null) {
return null;
}
return calendar.getTime();
}
public static Date getDate(int year, int month, int day, int hour, int minute, int second) {
return getCalendar(year, month, day, hour, minute, second).getTime();
}
public static Date getDate(long millis) {
return getCalendar(millis).getTime();
}
/**
* 获取当前日期,如2014-11-01
*
* @return
*/
public static String getNowDate() {
return formatDateTime(new Date(), DATE);
}
/**
* 获取当前时间,如12:23:23
*
* @return
*/
public static String getNowTime() {
return formatDateTime(new Date(), TIME);
}
/**
* 获取当前时间和日期,如2014-11-01 12:23:23
*
* @return
*/
public static String getNowDateTime() {
return formatDateTime(new Date(), DATETIME);
}
public static String formatDateTime(Date date, String pattern) throws IllegalArgumentException {
if ((date == null) || (pattern == null) || ("".equals(pattern.trim()))) {
return null;
}
return new SimpleDateFormat(pattern).format(date);
}
public static String formatDate(Date date) throws IllegalArgumentException {
if (date == null) {
return null;
}
return new SimpleDateFormat(DATE).format(date);
}
/***
* 格式化时间
*
* @param date
* 被格式化的时间字符串
* @param sourcePattern
* 被格式化时间字符串的格式
* @param targetPattern
* 格式化后的时间格式
* @return
* @throws ParseException
*/
public static String formatDateTime(String date, String sourcePattern, String targetPattern) throws ParseException {
if ((date == null) || ("".equals(date.trim())) || (sourcePattern == null) || ("".equals(sourcePattern.trim()))
|| (targetPattern == null) || "".equals(targetPattern.trim())) {
return null;
}
SimpleDateFormat ssdf = new SimpleDateFormat(sourcePattern);
SimpleDateFormat tsdf = new SimpleDateFormat(targetPattern);
return tsdf.format(ssdf.parse(date));
}
public static Date parseDateTime(String date, String pattern) throws ParseException {
if ((date == null) || ("".equals(date.trim())) || (pattern == null) || ("".equals(pattern.trim()))) {
return null;
}
return new SimpleDateFormat(pattern).parse(date);
}
public static Date parseDateTime(String date) throws ParseException {
return parseDateTime(date, DATETIME);
}
public static Date parseDate(String date) throws ParseException {
return parseDateTime(date, DATE);
}
/**
* 年份
*
* @param date
* @return
*/
public static int getYear(Date date) {
if (date == null) {
return -1;
}
Calendar calendar = getCalendar(date);
return calendar.get(Calendar.YEAR);
}
/**
* 月份
*
* @param date
* @return
*/
public static int getMonth(Date date) {
if (date == null) {
return -1;
}
Calendar calendar = getCalendar(date);
return calendar.get(Calendar.MONTH) + 1;
}
/**
* 本年的第几天
*
* @param date
* @return
*/
public static int getDayOfYear(Date date) {
if (date == null) {
return -1;
}
Calendar calendar = getCalendar(date);
return calendar.get(Calendar.DAY_OF_YEAR);
}
/**
* 本月的第几天
*
* @param date
* @return
*/
public static int getDayOfMonth(Date date) {
if (date == null) {
return -1;
}
Calendar calendar = getCalendar(date);
return calendar.get(Calendar.DAY_OF_MONTH);
}
/**
* 本周第几天(周几)
*
* @param date
* @return
*/
public static int getWeek(Date date) {
if (date == null) {
return -1;
}
Locale.setDefault(Locale.CHINA);
Calendar calendar = getCalendar(date);
return calendar.get(Calendar.DAY_OF_WEEK) - 1;
}
/**
* 返回小时
*
* @param date
* @return
*/
public static int getHour(Date date) {
Calendar calendar = getCalendar(date);
return calendar.get(Calendar.HOUR_OF_DAY);
}
/**
* 返回分钟
*
* @param date
* @return
*/
public static int getMinute(Date date) {
Calendar calendar = getCalendar(date);
return calendar.get(Calendar.MINUTE);
}
/**
* 返回秒钟
*
* @param date
* @return
*/
public static int getSecond(Date date) {
Calendar calendar = getCalendar(date);
return calendar.get(Calendar.SECOND);
}
/**
* 返回毫秒
*
* @param date
* @return
*/
public static long getMillis(Date date) {
Calendar calendar = getCalendar(date);
return calendar.getTimeInMillis();
}
/**
* 返回两个日期之间的相隔毫秒数
*
* @param d1
* @param d2
* @return
*/
public static long getMillisBetween(Date d1, Date d2) {
long s = getCalendar(d1).getTimeInMillis();
long e = getCalendar(d2).getTimeInMillis();
return Math.abs(e - s);
}
/**
* 返回两个日期之间的相隔分钟
*
* @param d1
* @param d2
* @return
*/
public static long getMiniteBetween(Date d1, Date d2) {
long s = getCalendar(d1).getTimeInMillis();
long e = getCalendar(d2).getTimeInMillis();
return Math.abs(e - s) / (1000 * 60);
}
/**
* 获取两个日期之间的天数:cal2 - cal1
*
* @param cal1
* @param cal2
* @return
*/
public static int getDaysBetween(Calendar cal1, Calendar cal2) {
boolean flag = true;
int days = cal2.get(Calendar.DAY_OF_YEAR) - cal1.get(Calendar.DAY_OF_YEAR);
int year2 = cal2.get(Calendar.YEAR);
int year1 = cal1.get(Calendar.YEAR);
if (year2 < year1) {
flag = false;
}
if (year1 != year2) {
do {
days += cal1.getActualMaximum(Calendar.DAY_OF_YEAR);
if (flag) {
cal1.add(Calendar.YEAR, 1);
} else {
cal1.add(Calendar.YEAR, -1);
}
} while (cal1.get(Calendar.YEAR) != year2);
}
if (flag) {
return days;
}
return -days;
}
public static int getDaysAbsBetween(Date date1, Date date2) {
return Math.abs(getDaysBetween(getCalendar(date1), getCalendar(date2)));
}
/**
* 获取两个日期之间的天数:date2 - date1
*
* @param date1
* @param date2
* @return
*/
public static int getDaysBetween(Date date1, Date date2) {
return getDaysBetween(getCalendar(date1), getCalendar(date2));
}
/**
* 获取两个日期之间相隔月份 cal2 - cal1
*
* @param cal1
* @param cal2
* @return
*/
public static int getMonthsBetween(Calendar cal1, Calendar cal2) {
int months = cal2.get(Calendar.MONTH) - cal1.get(Calendar.MONTH);
boolean flag = true;
int year2 = cal2.get(Calendar.YEAR);
int year1 = cal1.get(Calendar.YEAR);
if (year2 < year1)
flag = false;
if (year1 != year2) {
do {
months += cal1.getActualMaximum(Calendar.MONTH) + 1;
if (flag)
cal1.add(Calendar.YEAR, 1);
else
cal1.add(Calendar.YEAR, -1);
} while (cal1.get(Calendar.YEAR) != year2);
}
if (flag) {
return months;
}
return -months;
}
/**
* 获取两个日期相隔的月份 date2 - date1
*
* @param date1
* @param date2
* @return
*/
public static int getMonthsBetween(Date date1, Date date2) {
return getMonthsBetween(getCalendar(date1), getCalendar(date2));
}
/**
* 获取两个日期相隔年份 cal2 - cal1
*
* @param cal1
* @param cal2
* @return
*/
public static int getYearsBetween(Calendar cal1, Calendar cal2) {
int year2 = cal2.get(1);
int year1 = cal1.get(1);
return year2 - year1;
}
/**
* 获取两个日期相隔年份 date2 - date1
*
* @param date1
* @param date2
* @return
*/
public static int getYearsBetween(Date date1, Date date2) {
return getYearsBetween(getCalendar(date1), getCalendar(date2));
}
/**
* 获取给定日期n天后(前)的日期
*
* @param date
* @param days
* @return
*/
public static Date addDays(Date date, int n) {
if (date == null) {
return null;
}
Calendar calendar = GregorianCalendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, n);
return calendar.getTime();
}
/**
* 获取给定日期n个月后(前)的日期
*
* @param date
* @param months
* @return
*/
public static Date addMonths(Date date, int n) {
if (date == null) {
return null;
}
Calendar calendar = GregorianCalendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MONTH, n);
return calendar.getTime();
}
/**
* 获取给定日期n年后(前)的日期
*
* @param date
* @param years
* @return
*/
public static Date addYears(Date date, int n) {
if (date == null) {
return null;
}
Calendar calendar = GregorianCalendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.YEAR, n);
return calendar.getTime();
}
/**
* 获取给定日期n小时后(前)的日期
*
* @param date
* @param hours
* @return
*/
public static Date addHours(Date date, int n) {
Calendar calendar = GregorianCalendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.HOUR_OF_DAY, n);
return calendar.getTime();
}
/**
* 获取给定日期n分钟后(前)的日期
*
* @param date
* @param minutes
* @return
*/
public static Date addMinutes(Date date, int n) {
Calendar calendar = GregorianCalendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MINUTE, n);
return calendar.getTime();
}
/**
* 获取给定日期n秒后(前)的日期
*
* @param date
* @param seconds
* @return
*/
public static Date addSeconds(Date date, int n) {
Calendar calendar = GregorianCalendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.SECOND, n);
return calendar.getTime();
}
/**
* 获取上个月第一天
*
* @return
*/
public static Date getLastMonthFirstDay() {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, -1);
calendar.set(Calendar.DAY_OF_MONTH, 1);
return calendar.getTime();
}
/**
* 获取上个月最后一天
*
* @return
*/
public static Date getLastMonthLastDay() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.add(Calendar.DATE, -1);
return calendar.getTime();
}
/**
* 获取当前月第一天
*
* @return
*/
public static Date getCurMonthFirstDay() {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, 0);
calendar.set(Calendar.DAY_OF_MONTH, 1);
return calendar.getTime();
}
/**
* / 获取前月的最后一天
*
* @return
*/
public static Date getCurMonthLastDay() {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, 1);
calendar.set(Calendar.DAY_OF_MONTH, 0);
return calendar.getTime();
}
/**
* 获取给定时间所在月份的第一天
* @param date
* @return
*/
public static Date getSpecifyDateMonthFirstDay(Date date){
Calendar calendar = getCalendar(date);
calendar.add(Calendar.MONTH, 0);
calendar.set(Calendar.DAY_OF_MONTH, 1);
return calendar.getTime();
}
public static Date getSpecifyDateMonthLastDay(Date date){
Calendar calendar = getCalendar(date);
calendar.add(Calendar.MONTH, 1);
calendar.set(Calendar.DAY_OF_MONTH, 0);
return calendar.getTime();
}
/**
* 获取当前年份给定月份的天数
*
* @return
*/
public static int dayLenOfMonth(int m) {
if (m == 2) {
int year = getYear(new Date());
if (((year % 100 == 0) && (year % 400 == 0)) || ((year % 100 != 0) && (year % 4 == 0))) {
return 29;
}
return 28;
} else if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) {
return 31;
} else {
return 30;
}
}
public static void main(String[] args) {
}
}