Skip to content

Commit faf43b7

Browse files
committed
Merge branch 'master' of https://github.com/chenssy89/jutils
2 parents 9777b9c + d767df0 commit faf43b7

1 file changed

Lines changed: 37 additions & 64 deletions

File tree

src/com/JUtils/date/DateUtils.java

Lines changed: 37 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,15 @@ public static String getCurrentTime(){
8181
* @date Dec 30, 2013
8282
* @param format
8383
* @return Date
84-
* @throws Exception
8584
*/
86-
public static Date getCurrentDate(String format) throws Exception{
85+
public static Date getCurrentDate(String format){
8786
SimpleDateFormat sdf = getFormat(format);
8887
String dateS = getCurrentTime(format);
8988
Date date = null;
9089
try {
91-
date = sdf.parse(dateS);
90+
date = sdf.parse(dateS);
9291
} catch (ParseException e) {
93-
throw new Exception("时间转换出错..");
92+
e.printStackTrace();
9493
}
9594
return date;
9695
}
@@ -100,9 +99,8 @@ public static Date getCurrentDate(String format) throws Exception{
10099
* @author chenssy
101100
* @date Dec 30, 2013
102101
* @return Date
103-
* @throws Exception
104102
*/
105-
public static Date getCurrentDate() throws Exception{
103+
public static Date getCurrentDate(){
106104
return getCurrentDate(DateUtils.DATE_FORMAT2);
107105
}
108106

@@ -114,9 +112,8 @@ public static Date getCurrentDate() throws Exception{
114112
* @param date 为空时,默认为当前时间
115113
* @param format 默认格式为:yyyy-MM-dd HH:mm:ss
116114
* @return String
117-
* @throws Exception
118115
*/
119-
public static String addYearToDate(int year,Date date,String format) throws Exception{
116+
public static String addYearToDate(int year,Date date,String format){
120117
Calendar calender = getCalendar(date,format);
121118
SimpleDateFormat sdf = getFormat(format);
122119

@@ -133,9 +130,8 @@ public static String addYearToDate(int year,Date date,String format) throws Exce
133130
* @param date 为空时,默认为当前时间
134131
* @param format 默认格式为:yyyy-MM-dd HH:mm:ss
135132
* @return String
136-
* @throws Exception
137133
*/
138-
public static String addYearToDate(int year,String date,String format) throws Exception{
134+
public static String addYearToDate(int year,String date,String format){
139135
Date newDate = new Date();
140136
if(null != date && !"".equals(date)){
141137
newDate = string2Date(date, format);
@@ -152,9 +148,8 @@ public static String addYearToDate(int year,String date,String format) throws Ex
152148
* @param date 指定时间
153149
* @param format 指定格式 为空默认 yyyy-mm-dd HH:mm:ss
154150
* @return String
155-
* @throws Exception
156151
*/
157-
public static String addMothToDate(int month,Date date,String format) throws Exception{
152+
public static String addMothToDate(int month,Date date,String format) {
158153
Calendar calender = getCalendar(date,format);
159154
SimpleDateFormat sdf = getFormat(format);
160155

@@ -171,9 +166,8 @@ public static String addMothToDate(int month,Date date,String format) throws Exc
171166
* @param date 指定时间
172167
* @param format 指定格式 为空默认 yyyy-mm-dd HH:mm:ss
173168
* @return String
174-
* @throws Exception
175169
*/
176-
public static String addMothToDate(int month,String date,String format) throws Exception{
170+
public static String addMothToDate(int month,String date,String format) {
177171
Date newDate = new Date();
178172
if(null != date && !"".equals(date)){
179173
newDate = string2Date(date, format);
@@ -190,9 +184,8 @@ public static String addMothToDate(int month,String date,String format) throws E
190184
* @param date 指定日期
191185
* @param format 日期格式 为空默认 yyyy-mm-dd HH:mm:ss
192186
* @return String
193-
* @throws Exception
194187
*/
195-
public static String addDayToDate(int day,Date date,String format) throws Exception{
188+
public static String addDayToDate(int day,Date date,String format) {
196189
Calendar calendar = getCalendar(date, format);
197190
SimpleDateFormat sdf = getFormat(format);
198191

@@ -209,9 +202,8 @@ public static String addDayToDate(int day,Date date,String format) throws Except
209202
* @param date 指定日期
210203
* @param format 日期格式 为空默认 yyyy-mm-dd HH:mm:ss
211204
* @return String
212-
* @throws Exception
213205
*/
214-
public static String addDayToDate(int day,String date,String format) throws Exception{
206+
public static String addDayToDate(int day,String date,String format) {
215207
Date newDate = new Date();
216208
if(null != date && !"".equals(date)){
217209
newDate = string2Date(date, format);
@@ -228,9 +220,8 @@ public static String addDayToDate(int day,String date,String format) throws Exce
228220
* @param date 指定日期
229221
* @param format 日期格式 为空默认 yyyy-mm-dd HH:mm:ss
230222
* @return String
231-
* @throws Exception
232223
*/
233-
public static String addHourToDate(int hour,Date date,String format) throws Exception{
224+
public static String addHourToDate(int hour,Date date,String format) {
234225
Calendar calendar = getCalendar(date, format);
235226
SimpleDateFormat sdf = getFormat(format);
236227

@@ -247,9 +238,8 @@ public static String addHourToDate(int hour,Date date,String format) throws Exce
247238
* @param date 指定日期
248239
* @param format 日期格式 为空默认 yyyy-mm-dd HH:mm:ss
249240
* @return String
250-
* @throws Exception
251241
*/
252-
public static String addHourToDate(int hour,String date,String format) throws Exception{
242+
public static String addHourToDate(int hour,String date,String format) {
253243
Date newDate = new Date();
254244
if(null != date && !"".equals(date)){
255245
newDate = string2Date(date, format);
@@ -266,9 +256,8 @@ public static String addHourToDate(int hour,String date,String format) throws Ex
266256
* @param date 指定日期
267257
* @param format 日期格式 为空默认 yyyy-mm-dd HH:mm:ss
268258
* @return String
269-
* @throws Exception
270259
*/
271-
public static String addMinuteToDate(int minute,Date date,String format) throws Exception{
260+
public static String addMinuteToDate(int minute,Date date,String format) {
272261
Calendar calendar = getCalendar(date, format);
273262
SimpleDateFormat sdf = getFormat(format);
274263

@@ -285,9 +274,8 @@ public static String addMinuteToDate(int minute,Date date,String format) throws
285274
* @param date 指定日期
286275
* @param format 日期格式 为空默认 yyyy-mm-dd HH:mm:ss
287276
* @return String
288-
* @throws Exception
289277
*/
290-
public static String addMinuteToDate(int minute,String date,String format) throws Exception{
278+
public static String addMinuteToDate(int minute,String date,String format){
291279
Date newDate = new Date();
292280
if(null != date && !"".equals(date)){
293281
newDate = string2Date(date, format);
@@ -304,9 +292,8 @@ public static String addMinuteToDate(int minute,String date,String format) throw
304292
* @param date 指定日期
305293
* @param format 日期格式 为空默认 yyyy-mm-dd HH:mm:ss
306294
* @return String
307-
* @throws Exception
308295
*/
309-
public static String addSecondToDate(int second,Date date,String format) throws Exception{
296+
public static String addSecondToDate(int second,Date date,String format){
310297
Calendar calendar = getCalendar(date, format);
311298
SimpleDateFormat sdf = getFormat(format);
312299

@@ -325,7 +312,7 @@ public static String addSecondToDate(int second,Date date,String format) throws
325312
* @return String
326313
* @throws Exception
327314
*/
328-
public static String addSecondToDate(int second,String date,String format) throws Exception{
315+
public static String addSecondToDate(int second,String date,String format){
329316
Date newDate = new Date();
330317
if(null != date && !"".equals(date)){
331318
newDate = string2Date(date, format);
@@ -341,9 +328,8 @@ public static String addSecondToDate(int second,String date,String format) throw
341328
* @param date 时间
342329
* @param format 格式
343330
* @return Calendar
344-
* @throws Exception
345331
*/
346-
public static Calendar getCalendar(Date date,String format) throws Exception{
332+
public static Calendar getCalendar(Date date,String format){
347333
if(date == null){
348334
date = getCurrentDate(format);
349335
}
@@ -376,20 +362,20 @@ private static SimpleDateFormat getFormat(String format){
376362
* @param value 需要转换的字符串
377363
* @param format 日期格式
378364
* @return Date
379-
* @throws Exception
380365
*/
381-
public static Date string2Date(String value,String format) throws Exception{
366+
public static Date string2Date(String value,String format){
382367
if(value == null || "".equals(value)){
383368
return null;
384369
}
385370

386371
SimpleDateFormat sdf = getFormat(format);
387372
Date date = null;
388-
value = formatDate(value, format);
373+
389374
try {
375+
value = formatDate(value, format);
390376
date = sdf.parse(value);
391-
} catch (ParseException e) {
392-
throw new Exception("时间转换出错..");
377+
} catch (Exception e) {
378+
e.printStackTrace();
393379
}
394380
return date;
395381
}
@@ -419,10 +405,8 @@ public static String date2String(Date value,String format){
419405
* @param value 时间
420406
* @param format 指定格式
421407
* @return
422-
* @throws Exception
423-
* @throws ParseException
424408
*/
425-
public static String formatDate(String date,String format) throws Exception{
409+
public static String formatDate(String date,String format) {
426410
if(ValidateHelper.isEmpty(date) || ValidateHelper.isEmpty(format)){
427411
return "";
428412
}
@@ -483,9 +467,8 @@ public static String formatDate(String date,String format) throws Exception{
483467
*
484468
* @param value
485469
* @return
486-
* @throws Exception
487470
*/
488-
public static String formatDate(String value) throws Exception{
471+
public static String formatDate(String value){
489472
return getFormat(DateUtils.DATE_FORMAT2).format(string2Date(value, DateUtils.DATE_FORMAT2));
490473
}
491474

@@ -508,9 +491,8 @@ public static int getCurrentYear(Date value){
508491
* @date Dec 31, 2013
509492
* @param value 日期
510493
* @return int
511-
* @throws Exception
512494
*/
513-
public static int getCurrentYear(String value) throws Exception{
495+
public static int getCurrentYear(String value) {
514496
Date date = string2Date(value, DateUtils.DATE_YEAR);
515497
Calendar calendar = getCalendar(date, DateUtils.DATE_YEAR);
516498
return calendar.get(Calendar.YEAR);
@@ -534,9 +516,8 @@ public static int getCurrentMonth(Date value){
534516
* @date Dec 31, 2013
535517
* @param value 日期
536518
* @return int
537-
* @throws Exception
538519
*/
539-
public static int getCurrentMonth(String value) throws Exception{
520+
public static int getCurrentMonth(String value) {
540521
Date date = string2Date(value, DateUtils.DATE_MONTH);
541522
Calendar calendar = getCalendar(date, DateUtils.DATE_MONTH);
542523

@@ -561,9 +542,8 @@ public static int getCurrentDay(Date value){
561542
* @date Dec 31, 2013
562543
* @param value 日期
563544
* @return int
564-
* @throws Exception
565545
*/
566-
public static int getCurrentDay(String value) throws Exception{
546+
public static int getCurrentDay(String value){
567547
Date date = string2Date(value, DateUtils.DATE_DAY);
568548
Calendar calendar = getCalendar(date, DateUtils.DATE_DAY);
569549

@@ -576,9 +556,8 @@ public static int getCurrentDay(String value) throws Exception{
576556
* @date Dec 31, 2013
577557
* @param value 日期
578558
* @return String
579-
* @throws Exception
580559
*/
581-
public static String getCurrentWeek(Date value) throws Exception{
560+
public static String getCurrentWeek(Date value) {
582561
Calendar calendar = getCalendar(value, DateUtils.DATE_FORMAT1);
583562
int weekIndex = calendar.get(Calendar.DAY_OF_WEEK) - 1 < 0 ? 0 : calendar.get(Calendar.DAY_OF_WEEK) - 1;
584563

@@ -591,9 +570,8 @@ public static String getCurrentWeek(Date value) throws Exception{
591570
* @date Dec 31, 2013
592571
* @param value 日期
593572
* @return String
594-
* @throws Exception
595573
*/
596-
public static String getCurrentWeek(String value) throws Exception{
574+
public static String getCurrentWeek(String value) {
597575
Date date = string2Date(value, DateUtils.DATE_FORMAT1);
598576
return getCurrentWeek(date);
599577
}
@@ -617,9 +595,8 @@ public static int getCurrentHour(Date value){
617595
* @param value 日期
618596
* @return
619597
* @return int
620-
* @throws Exception
621598
*/
622-
public static int getCurrentHour(String value) throws Exception{
599+
public static int getCurrentHour(String value) {
623600
Date date = string2Date(value, DateUtils.DATE_HOUR);
624601
Calendar calendar = getCalendar(date, DateUtils.DATE_HOUR);
625602

@@ -644,9 +621,8 @@ public static int getCurrentMinute(Date value){
644621
* @date Dec 31, 2013
645622
* @param value 日期
646623
* @return int
647-
* @throws Exception
648624
*/
649-
public static int getCurrentMinute(String value) throws Exception{
625+
public static int getCurrentMinute(String value){
650626
Date date = string2Date(value, DateUtils.DATE_MINUTE);
651627
Calendar calendar = getCalendar(date, DateUtils.DATE_MINUTE);
652628

@@ -666,9 +642,8 @@ public static int getCurrentMinute(String value) throws Exception{
666642
* @param endDay 被比较的时间 为空(null)则为当前时间
667643
* @param stype 返回值类型 0为多少天,1为多少个月,2为多少年
668644
* @return int
669-
* @throws Exception
670645
*/
671-
public static int compareDate(String startDay,String endDay,int stype) throws Exception{
646+
public static int compareDate(String startDay,String endDay,int stype) {
672647
int n = 0;
673648
startDay = formatDate(startDay, "yyyy-MM-dd");
674649
endDay = formatDate(endDay, "yyyy-MM-dd");
@@ -688,8 +663,8 @@ public static int compareDate(String startDay,String endDay,int stype) throws Ex
688663
try {
689664
c1.setTime(df.parse(startDay));
690665
c2.setTime(df.parse(endDay));
691-
} catch (Exception e) {
692-
throw new Exception("时间转换出错..");
666+
} catch (Exception e) {
667+
e.printStackTrace();
693668
}
694669
while (!c1.after(c2)) { // 循环对比,直到相等,n 就是所要的结果
695670
n++;
@@ -715,9 +690,8 @@ public static int compareDate(String startDay,String endDay,int stype) throws Ex
715690
* @param endTime 需要被比较的时间 若为空则默认当前时间
716691
* @param type 1:小时 2:分钟 3:秒
717692
* @return int
718-
* @throws Exception
719693
*/
720-
public static int compareTime(String startTime , String endTime , int type) throws Exception{
694+
public static int compareTime(String startTime , String endTime , int type) {
721695
//endTime是否为空,为空默认当前时间
722696
if(endTime == null || "".equals(endTime)){
723697
endTime = getCurrentTime();
@@ -739,7 +713,7 @@ else if(type == 3){
739713
value = (int) (between % 60 / 60);
740714
}
741715
} catch (ParseException e) {
742-
throw new Exception("时间转换出错..");
716+
e.printStackTrace();
743717
}
744718
return value;
745719
}
@@ -784,9 +758,8 @@ public static int compare(String date1, String date2,String format) {
784758
* @param value
785759
* @param format
786760
* @return
787-
* @throws Exception
788761
*/
789-
public static Timestamp string2Timestamp(String value) throws Exception{
762+
public static Timestamp string2Timestamp(String value){
790763
Timestamp ts = new Timestamp(System.currentTimeMillis());
791764
ts = Timestamp.valueOf(value);
792765
return ts;

0 commit comments

Comments
 (0)