This repository has been archived by the owner on Jan 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
DateUtils.java
1263 lines (1117 loc) · 34.4 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
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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package org.xueyao.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.text.DateFormat;
/**
* 处理日期时间的工具类。
*
* @author xuan
* @version $Revision: 1.0 $, $Date: 2012-11-22 上午9:46:12 $
*/
public abstract class DateUtils {
private static final int[] DAY_OF_MONTH = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
private static final String[] weeks = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
/**
* 取得指定天数后的时间
*
* @param date 基准时间
* @param dayAmount 指定天数,允许为负数
* @return 指定天数后的时间
*/
public static Date addDay(Date date, int dayAmount) {
if (date == null) {
return null;
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DATE, dayAmount);
return calendar.getTime();
}
/**
* 取得指定小时数后的时间
*
* @param date 基准时间
* @param hourAmount 指定小时数,允许为负数
* @return 指定小时数后的时间
*/
public static Date addHour(Date date, int hourAmount) {
if (date == null) {
return null;
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.HOUR, hourAmount);
return calendar.getTime();
}
/**
* 取得指定分钟数后的时间
*
* @param date 基准时间
* @param minuteAmount 指定分钟数,允许为负数
* @return 指定分钟数后的时间
*/
public static Date addMinute(Date date, int minuteAmount) {
if (date == null) {
return null;
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MINUTE, minuteAmount);
return calendar.getTime();
}
/**
* 比较两日期对象中的小时和分钟部分的大小.
*
* @param date 日期对象1, 如果为 <code>null</code> 会以当前时间的日期对象代替
* @param anotherDate 日期对象2, 如果为 <code>null</code> 会以当前时间的日期对象代替
* @return 如果日期对象1大于日期对象2, 则返回大于0的数; 反之返回小于0的数; 如果两日期对象相等, 则返回0.
*/
public static int compareHourAndMinute(Date date, Date anotherDate) {
if (date == null) {
date = new Date();
}
if (anotherDate == null) {
anotherDate = new Date();
}
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int hourOfDay1 = cal.get(Calendar.HOUR_OF_DAY);
int minute1 = cal.get(Calendar.MINUTE);
cal.setTime(anotherDate);
int hourOfDay2 = cal.get(Calendar.HOUR_OF_DAY);
int minute2 = cal.get(Calendar.MINUTE);
if (hourOfDay1 > hourOfDay2) {
return 1;
} else if (hourOfDay1 == hourOfDay2) {
// 小时相等就比较分钟
return minute1 > minute2 ? 1 : (minute1 == minute2 ? 0 : -1);
} else {
return -1;
}
}
/**
* 比较两日期对象的大小, 忽略秒, 只精确到分钟.
*
* @param date 日期对象1, 如果为 <code>null</code> 会以当前时间的日期对象代替
* @param anotherDate 日期对象2, 如果为 <code>null</code> 会以当前时间的日期对象代替
* @return 如果日期对象1大于日期对象2, 则返回大于0的数; 反之返回小于0的数; 如果两日期对象相等, 则返回0.
*/
public static int compareIgnoreSecond(Date date, Date anotherDate) {
if (date == null) {
date = new Date();
}
if (anotherDate == null) {
anotherDate = new Date();
}
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
date = cal.getTime();
cal.setTime(anotherDate);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
anotherDate = cal.getTime();
return date.compareTo(anotherDate);
}
/**
* 取得当前时间的字符串表示,格式为2006-01-10 20:56:30.756
*
* @return 当前时间的字符串表示
*/
public static String currentDate2String() {
return date2String(new Date());
}
/**
* 取得当前时间的字符串表示,格式为2006-01-10
*
* @return 当前时间的字符串表示
*/
public static String currentDate2StringByDay() {
return date2StringByDay(new Date());
}
/**
* 取得今天的最后一个时刻
*
* @return 今天的最后一个时刻
*/
public static Date currentEndDate() {
return getEndDate(new Date());
}
/**
* 取得今天的第一个时刻
*
* @return 今天的第一个时刻
*/
public static Date currentStartDate() {
return getStartDate(new Date());
}
/**
* 把时间转换成字符串,格式为2006-01-10 20:56:30.756
*
* @param date 时间
* @return 时间字符串
*/
public static String date2String(Date date) {
return date2String(date, "yyyy-MM-dd HH:mm:ss.SSS");
}
/**
* 按照指定格式把时间转换成字符串,格式的写法类似yyyy-MM-dd HH:mm:ss.SSS
*
* @param date 时间
* @param pattern 格式
* @return 时间字符串
*/
public static String date2String(Date date, String pattern) {
if (date == null) {
return null;
}
return (new SimpleDateFormat(pattern)).format(date);
}
/**
* 把时间转换成字符串,格式为2006-01-10
*
* @param date 时间
* @return 时间字符串
*/
public static String date2StringByDay(Date date) {
return date2String(date, "yyyy-MM-dd");
}
/**
* 把时间转换成字符串,格式为2006-01-10 20:56
*
* @param date 时间
* @return 时间字符串
*/
public static String date2StringByMinute(Date date) {
return date2String(date, "yyyy-MM-dd HH:mm");
}
/**
* 把时间转换成字符串,格式为2006-01-10 20:56:30
*
* @param date 时间
* @return 时间字符串
*/
public static String date2StringBySecond(Date date) {
return date2String(date, "yyyy-MM-dd HH:mm:ss");
}
/**
* 根据某星期几的英文名称来获取该星期几的中文数. <br>
* e.g. <li>monday -> 一</li> <li>sunday -> 日</li>
*
* @param englishWeekName 星期的英文名称
* @return 星期的中文数
*/
public static String getChineseWeekNumber(String englishWeekName) {
if ("monday".equalsIgnoreCase(englishWeekName)) {
return "一";
}
if ("tuesday".equalsIgnoreCase(englishWeekName)) {
return "二";
}
if ("wednesday".equalsIgnoreCase(englishWeekName)) {
return "三";
}
if ("thursday".equalsIgnoreCase(englishWeekName)) {
return "四";
}
if ("friday".equalsIgnoreCase(englishWeekName)) {
return "五";
}
if ("saturday".equalsIgnoreCase(englishWeekName)) {
return "六";
}
if ("sunday".equalsIgnoreCase(englishWeekName)) {
return "日";
}
return null;
}
/**
* 根据指定的年, 月, 日等参数获取日期对象.
*
* @param year 年
* @param month 月
* @param date 日
* @return 对应的日期对象
*/
public static Date getDate(int year, int month, int date) {
return getDate(year, month, date, 0, 0);
}
/**
* 根据指定的年, 月, 日, 时, 分等参数获取日期对象.
*
* @param year 年
* @param month 月
* @param date 日
* @param hourOfDay 时(24小时制)
* @param minute 分
* @return 对应的日期对象
*/
public static Date getDate(int year, int month, int date, int hourOfDay, int minute) {
return getDate(year, month, date, hourOfDay, minute, 0);
}
/**
* 根据指定的年, 月, 日, 时, 分, 秒等参数获取日期对象.
*
* @param year 年
* @param month 月
* @param date 日
* @param hourOfDay 时(24小时制)
* @param minute 分
* @param second 秒
* @return 对应的日期对象
*/
public static Date getDate(int year, int month, int date, int hourOfDay, int minute, int second) {
Calendar cal = Calendar.getInstance();
cal.set(year, month - 1, date, hourOfDay, minute, second);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
/**
* 取得某个日期是星期几,星期日是1,依此类推
*
* @param date 日期
* @return 星期几
*/
public static int getDayOfWeek(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.DAY_OF_WEEK);
}
/**
* 获取某天的结束时间, e.g. 2005-10-01 23:59:59.999
*
* @param date 日期对象
* @return 该天的结束时间
*/
public static Date getEndDate(Date date) {
if (date == null) {
return null;
}
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
cal.set(Calendar.MILLISECOND, 999);
return cal.getTime();
}
/**
* 取得一个月最多的天数
*
* @param year 年份
* @param month 月份,0表示1月,依此类推
* @return 最多的天数
*/
public static int getMaxDayOfMonth(int year, int month) {
if (month == 1 && isLeapYear(year)) {
return 29;
}
return DAY_OF_MONTH[month];
}
/**
* 得到指定日期的下一天
*
* @param date 日期对象
* @return 同一时间的下一天的日期对象
*/
public static Date getNextDay(Date date) {
return addDay(date, 1);
}
/**
* 获取某天的起始时间, e.g. 2005-10-01 00:00:00.000
*
* @param date 日期对象
* @return 该天的起始时间
*/
public static Date getStartDate(Date date) {
if (date == null) {
return null;
}
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
/**
* 根据日期对象来获取日期中的时间(HH:mm:ss).
*
* @param date 日期对象
* @return 时间字符串, 格式为: HH:mm:ss
*/
public static String getTime(Date date) {
if (date == null) {
return null;
}
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
return format.format(date);
}
/**
* 根据日期对象来获取日期中的时间(HH:mm).
*
* @param date 日期对象
* @return 时间字符串, 格式为: HH:mm
*/
public static String getTimeIgnoreSecond(Date date) {
if (date == null) {
return null;
}
SimpleDateFormat format = new SimpleDateFormat("HH:mm");
return format.format(date);
}
/**
* 判断是否是闰年
*
* @param year 年份
* @return 是true,否则false
*/
public static boolean isLeapYear(int year) {
Calendar calendar = Calendar.getInstance();
return ((GregorianCalendar) calendar).isLeapYear(year);
}
/**
* 把字符串转换成日期,格式为2006-01-10
*
* @param str 字符串
* @return 日期
*/
public static Date string2Date(String str) {
return string2Date(str, "yyyy-MM-dd");
}
/**
* 按照指定的格式把字符串转换成时间,格式的写法类似yyyy-MM-dd HH:mm:ss.SSS
*
* @param str 字符串
* @param pattern 格式
* @return 时间
*/
public static Date string2Date(String str, String pattern) {
if (Validators.isEmpty(str)) {
return null;
}
SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
Date date = null;
try {
date = dateFormat.parse(str);
} catch (ParseException e) {
// ignore
}
return date;
}
/**
* 把字符串转换成日期,格式为2006-01-10 20:56:30
*
* @param str 字符串
* @return 日期
*/
public static Date string2DateTime(String str) {
return string2Date(str, "yyyy-MM-dd HH:mm:ss");
}
/**
* 取得一年中的第几周。
*
* @param date
* @return
*/
public static int getWeekOfYear(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.WEEK_OF_YEAR);
}
/**
* 获取上周的指定星期的日期。
*
* @param dayOfWeek 星期几,取值范围是 {@link Calendar#MONDAY} - {@link Calendar#SUNDAY}
*/
public static Date getDateOfPreviousWeek(int dayOfWeek) {
if (dayOfWeek > 7 || dayOfWeek < 1) {
throw new IllegalArgumentException("参数必须是1-7之间的数字");
}
return getDateOfRange(dayOfWeek, -7);
}
/**
* 获取本周的指定星期的日期。
*
* @param dayOfWeek 星期几,取值范围是 {@link Calendar#MONDAY} - {@link Calendar#SUNDAY}
*/
public static Date getDateOfCurrentWeek(int dayOfWeek) {
if (dayOfWeek > 7 || dayOfWeek < 1) {
throw new IllegalArgumentException("参数必须是1-7之间的数字");
}
return getDateOfRange(dayOfWeek, 0);
}
/**
* 获取下周的指定星期的日期。
*
* @param dayOfWeek 星期几,取值范围是 {@link Calendar#MONDAY} - {@link Calendar#SUNDAY}
*/
public static Date getDateOfNextWeek(int dayOfWeek) {
if (dayOfWeek > 7 || dayOfWeek < 1) {
throw new IllegalArgumentException("参数必须是1-7之间的数字");
}
return getDateOfRange(dayOfWeek, 7);
}
private static Date getDateOfRange(int dayOfWeek, int dayOfRange) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_WEEK, dayOfWeek);
cal.set(Calendar.DATE, cal.get(Calendar.DATE) + dayOfRange);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
/**
* 根据指定格式获取当前时间
* @author chenssy
* @date Dec 27, 2013
* @param format
* @return String
*/
public static String getCurrentTime(String format){
SimpleDateFormat sdf = DateFormatUtils.getFormat(format);
Date date = new Date();
return sdf.format(date);
}
/**
* 获取当前时间,格式为:yyyy-MM-dd HH:mm:ss
* @author chenssy
* @date Dec 27, 2013
* @return String
*/
public static String getCurrentTime(){
return getCurrentTime(DateFormatUtils.DATE_FORMAT2);
}
/**
* 获取指定格式的当前时间:为空时格式为yyyy-mm-dd HH:mm:ss
* @author chenssy
* @date Dec 30, 2013
* @param format
* @return Date
*/
public static Date getCurrentDate(String format){
SimpleDateFormat sdf = DateFormatUtils.getFormat(format);
String dateS = getCurrentTime(format);
Date date = null;
try {
date = sdf.parse(dateS);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
/**
* 获取当前时间,格式为yyyy-MM-dd HH:mm:ss
* @author chenssy
* @date Dec 30, 2013
* @return Date
*/
public static Date getCurrentDate(){
return getCurrentDate(DateFormatUtils.DATE_FORMAT2);
}
/**
* 给指定日期加入年份,为空时默认当前时间
* @author chenssy
* @date Dec 30, 2013
* @param year 年份 正数相加、负数相减
* @param date 为空时,默认为当前时间
* @param format 默认格式为:yyyy-MM-dd HH:mm:ss
* @return String
*/
public static String addYearToDate(int year,Date date,String format){
Calendar calender = getCalendar(date,format);
SimpleDateFormat sdf = DateFormatUtils.getFormat(format);
calender.add(Calendar.YEAR, year);
return sdf.format(calender.getTime());
}
/**
* 给指定日期加入年份,为空时默认当前时间
* @author chenssy
* @date Dec 30, 2013
* @param year 年份 正数相加、负数相减
* @param date 为空时,默认为当前时间
* @param format 默认格式为:yyyy-MM-dd HH:mm:ss
* @return String
*/
public static String addYearToDate(int year,String date,String format){
Date newDate = new Date();
if(null != date && !"".equals(date)){
newDate = string2Date(date, format);
}
return addYearToDate(year, newDate, format);
}
/**
* 给指定日期增加月份 为空时默认当前时间
* @author chenssy
* @date Dec 30, 2013
* @param month 增加月份 正数相加、负数相减
* @param date 指定时间
* @param format 指定格式 为空默认 yyyy-mm-dd HH:mm:ss
* @return String
*/
public static String addMothToDate(int month,Date date,String format) {
Calendar calender = getCalendar(date,format);
SimpleDateFormat sdf = DateFormatUtils.getFormat(format);
calender.add(Calendar.MONTH, month);
return sdf.format(calender.getTime());
}
/**
* 给指定日期增加月份 为空时默认当前时间
* @author chenssy
* @date Dec 30, 2013
* @param month 增加月份 正数相加、负数相减
* @param date 指定时间
* @param format 指定格式 为空默认 yyyy-mm-dd HH:mm:ss
* @return String
*/
public static String addMothToDate(int month,String date,String format) {
Date newDate = new Date();
if(null != date && !"".equals(date)){
newDate = string2Date(date, format);
}
return addMothToDate(month, newDate, format);
}
/**
* 给指定日期增加天数,为空时默认当前时间
* @author chenssy
* @date Dec 31, 2013
* @param day 增加天数 正数相加、负数相减
* @param date 指定日期
* @param format 日期格式 为空默认 yyyy-mm-dd HH:mm:ss
* @return String
*/
public static String addDayToDate(int day,Date date,String format) {
Calendar calendar = getCalendar(date, format);
SimpleDateFormat sdf = DateFormatUtils.getFormat(format);
calendar.add(Calendar.DATE, day);
return sdf.format(calendar.getTime());
}
/**
* 给指定日期增加天数,为空时默认当前时间
* @author chenssy
* @date Dec 31, 2013
* @param day 增加天数 正数相加、负数相减
* @param date 指定日期
* @param format 日期格式 为空默认 yyyy-mm-dd HH:mm:ss
* @return String
*/
public static String addDayToDate(int day,String date,String format) {
Date newDate = new Date();
if(null != date && !"".equals(date)){
newDate = string2Date(date, format);
}
return addDayToDate(day, newDate, format);
}
/**
* 给指定日期增加小时,为空时默认当前时间
* @author chenssy
* @date Dec 31, 2013
* @param hour 增加小时 正数相加、负数相减
* @param date 指定日期
* @param format 日期格式 为空默认 yyyy-mm-dd HH:mm:ss
* @return String
*/
public static String addHourToDate(int hour,Date date,String format) {
Calendar calendar = getCalendar(date, format);
SimpleDateFormat sdf = DateFormatUtils.getFormat(format);
calendar.add(Calendar.HOUR, hour);
return sdf.format(calendar.getTime());
}
/**
* 给指定日期增加小时,为空时默认当前时间
* @author chenssy
* @date Dec 31, 2013
* @param hour 增加小时 正数相加、负数相减
* @param date 指定日期
* @param format 日期格式 为空默认 yyyy-mm-dd HH:mm:ss
* @return String
*/
public static String addHourToDate(int hour,String date,String format) {
Date newDate = new Date();
if(null != date && !"".equals(date)){
newDate = string2Date(date, format);
}
return addHourToDate(hour, newDate, format);
}
/**
* 给指定的日期增加分钟,为空时默认当前时间
* @author chenssy
* @date Dec 31, 2013
* @param minute 增加分钟 正数相加、负数相减
* @param date 指定日期
* @param format 日期格式 为空默认 yyyy-mm-dd HH:mm:ss
* @return String
*/
public static String addMinuteToDate(int minute,Date date,String format) {
Calendar calendar = getCalendar(date, format);
SimpleDateFormat sdf = DateFormatUtils.getFormat(format);
calendar.add(Calendar.MINUTE, minute);
return sdf.format(calendar.getTime());
}
/**
* 给指定的日期增加分钟,为空时默认当前时间
* @author chenssy
* @date Dec 31, 2013
* @param minute 增加分钟 正数相加、负数相减
* @param date 指定日期
* @param format 日期格式 为空默认 yyyy-mm-dd HH:mm:ss
* @return String
*/
public static String addMinuteToDate(int minute,String date,String format){
Date newDate = new Date();
if(null != date && !"".equals(date)){
newDate = string2Date(date, format);
}
return addMinuteToDate(minute, newDate, format);
}
/**
* 给指定日期增加秒,为空时默认当前时间
* @author chenssy
* @date Dec 31, 2013
* @param second 增加秒 正数相加、负数相减
* @param date 指定日期
* @param format 日期格式 为空默认 yyyy-mm-dd HH:mm:ss
* @return String
*/
public static String addSecondToDate(int second,Date date,String format){
Calendar calendar = getCalendar(date, format);
SimpleDateFormat sdf = DateFormatUtils.getFormat(format);
calendar.add(Calendar.SECOND, second);
return sdf.format(calendar.getTime());
}
/**
* 给指定日期增加秒,为空时默认当前时间
* @author chenssy
* @date Dec 31, 2013
* @param second 增加秒 正数相加、负数相减
* @param date 指定日期
* @param format 日期格式 为空默认 yyyy-mm-dd HH:mm:ss
* @return String
* @throws Exception
*/
public static String addSecondToDate(int second,String date,String format){
Date newDate = new Date();
if(null != date && !"".equals(date)){
newDate = string2Date(date, format);
}
return addSecondToDate(second, newDate, format);
}
/**
* 获取指定格式指定时间的日历
* @author chenssy
* @date Dec 30, 2013
* @param date 时间
* @param format 格式
* @return Calendar
*/
public static Calendar getCalendar(Date date,String format){
if(date == null){
date = getCurrentDate(format);
}
Calendar calender = Calendar.getInstance();
calender.setTime(date);
return calender;
}
/**
* 字符串转换为日期,日期格式为
*
* @author : chenssy
* @date : 2016年5月31日 下午5:20:22
*
* @param value
* @return
*/
public static Date string2Date(String value){
if(value == null || "".equals(value)){
return null;
}
SimpleDateFormat sdf = DateFormatUtils.getFormat(DateFormatUtils.DATE_FORMAT2);
Date date = null;
try {
value = DateFormatUtils.formatDate(value, DateFormatUtils.DATE_FORMAT2);
date = sdf.parse(value);
} catch (Exception e) {
e.printStackTrace();
}
return date;
}
/**
* 将字符串(格式符合规范)转换成Date
* @author chenssy
* @date Dec 31, 2013
* @param value 需要转换的字符串
* @param format 日期格式
* @return Date
*/
public static Date string2Date(String value,String format){
if(value == null || "".equals(value)){
return null;
}
SimpleDateFormat sdf = DateFormatUtils.getFormat(format);
Date date = null;
try {
value = DateFormatUtils.formatDate(value, format);
date = sdf.parse(value);
} catch (Exception e) {
e.printStackTrace();
}
return date;
}
/**
* 将日期格式转换成String
* @author chenssy
* @date Dec 31, 2013
*
* @param value 需要转换的日期
* @param format 日期格式
* @return String
*/
public static String date2String(Date value,String format){
if(value == null){
return null;
}
SimpleDateFormat sdf = DateFormatUtils.getFormat(format);
return sdf.format(value);
}
/**
* 日期转换为字符串
*
* @author : chenssy
* @date : 2016年5月31日 下午5:21:38
*
* @param value
* @return
*/
public static String date2String(Date value){
if(value == null){
return null;
}
SimpleDateFormat sdf = DateFormatUtils.getFormat(DateFormatUtils.DATE_FORMAT2);
return sdf.format(value);
}
/**
* 获取指定日期的年份
* @author chenssy
* @date Dec 31, 2013
* @param value 日期
* @return int
*/
public static int getCurrentYear(Date value){
String date = date2String(value, DateFormatUtils.DATE_YEAR);
return Integer.valueOf(date);
}
/**
* 获取指定日期的年份
* @author chenssy
* @date Dec 31, 2013
* @param value 日期
* @return int
*/
public static int getCurrentYear(String value) {
Date date = string2Date(value, DateFormatUtils.DATE_YEAR);
Calendar calendar = getCalendar(date, DateFormatUtils.DATE_YEAR);
return calendar.get(Calendar.YEAR);
}
/**
* 获取指定日期的月份
* @author chenssy
* @date Dec 31, 2013
* @param value 日期
* @return int
*/
public static int getCurrentMonth(Date value){
String date = date2String(value, DateFormatUtils.DATE_MONTH);
return Integer.valueOf(date);
}
/**
* 获取指定日期的月份
* @author chenssy
* @date Dec 31, 2013
* @param value 日期
* @return int
*/
public static int getCurrentMonth(String value) {
Date date = string2Date(value, DateFormatUtils.DATE_MONTH);
Calendar calendar = getCalendar(date, DateFormatUtils.DATE_MONTH);
return calendar.get(Calendar.MONTH);
}
/**
* 获取指定日期的天份
* @author chenssy
* @date Dec 31, 2013
* @param value 日期
* @return int
*/
public static int getCurrentDay(Date value){
String date = date2String(value, DateFormatUtils.DATE_DAY);
return Integer.valueOf(date);
}
/**
* 获取指定日期的天份
* @author chenssy
* @date Dec 31, 2013
* @param value 日期
* @return int
*/
public static int getCurrentDay(String value){
Date date = string2Date(value, DateFormatUtils.DATE_DAY);
Calendar calendar = getCalendar(date, DateFormatUtils.DATE_DAY);
return calendar.get(Calendar.DATE);
}
/**
* 获取当前日期为星期几
* @author chenssy
* @date Dec 31, 2013
* @param value 日期
* @return String
*/