date 时间工具类----持续更新
常用方法:
1、String date2String(Date dDate, String sFormat): 按指定的格式 日期->字符串,默认为yyyy-MM-dd
2、Date string2Date(String str, String format):按指定的格式 字符串->日期,默认为yyyy-MM-dd HH:mm:ss
3、Date string2Date(String s):按字符串的长度来自动设置格式 字符串->日期
4、String getCurrentYearMonth() :返回当天所在的年月 ,yyyyMM
5、Date addMonths(Date dateInput, int numberOfMonth):添加n月份到一个日期对象,为负数则计算向前n个月
6、Date nextDate(Date date, int day):取得后day天数的日期,day为负数表示以前的日期
7、String getLastDateOfMonth(String strYYYYMMDD):取得指定日期对应月的最后一天日期
8、int getDaysOfMonth(int yyyy, int mm):根据年月取得当月的天数
9、Date getOffsetDate(String strYYYYMMDD, int nOffsetNum,String strOffsetUnit):将指定日期偏移一段时间
10、String[] getDayList(String beginDate, String endDate):获得从开始日期到截止日期间所有有效日期的字符数组,参数格式为YYYYmmdd
11、String formatDateToCN(String date):把日期字符转换为中文含义的日期字符
12、Date getDateObj(int year, int month, int day):通过年月日生成时间对象
代码如下:
1 import java.text.DateFormat;
2 import java.text.ParseException;
3 import java.text.SimpleDateFormat;
4 import java.util.ArrayList;
5 import java.util.Calendar;
6 import java.util.Date;
7 import java.util.GregorianCalendar;
8 import java.util.List;
9 import java.util.Vector;
10
11 /**
12 *
13 * <p>
14 * Title: DateUtil.java
15 * </p>
16 * <p>
17 * Description: 对日期的处理类,提供一些常用的对日期进行处理的方法
18 * </p>
19 * @version 1.0
20 *
21 */
22 public class DateUtil {
23
24 /**
25 * 预定义的日期格式:yyyy-MM-dd HH:mm:ss
26 */
27 public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
28
29 /**
30 * 预定义的日期格式:yyyyMMdd_HHmmss
31 */
32 public static final String YYYYMMDD_HHMMSS = "yyyyMMdd_HHmmss";
33
34 /**
35 * 预定义的日期格式:yyyy-MM-dd HH:mm
36 */
37 public static final String YYYYMMDD_HHMM = "yyyyMMdd HHmm";
38
39 /**
40 * 预定义的日期格式:yyyy-MM-dd
41 */
42 public static final String YYYY_MM_DD = "yyyy-MM-dd";
43
44 /**
45 * 预定义的日期格式:yyyy-MM
46 */
47 public static final String YYYY_MM = "yyyy-MM";
48
49 /**
50 * 预定义的日期格式:yyyyMMdd
51 */
52 public static final String YYYYMMDD = "yyyyMMdd";
53
54 /**
55 * 预定义的日期格式:yyyyMM
56 */
57 public static final String YYYYMM = "yyyyMM";
58
59 /**
60 * 预定义的日期格式:yyyy
61 */
62 public static final String YYYY = "yyyy";
63
64 /**
65 * 预定义的日期格式:MM-dd
66 */
67 public static final String MM_dd = "MM-dd";
68
69 /**
70 * 预定义的日期格式:MMdd
71 */
72 public static final String MMdd = "MMdd";
73
74 /**
75 * 预定义的日期格式:MM
76 */
77 public static final String MM = "MM";
78
79 /**
80 * 预定义的日期格式:HH:mm
81 */
82 public static final String HH_mm = "HH:mm";
83
84 /**
85 * 预定义的日期格式:HHmm
86 */
87 public static final String HHmm = "HHmm";
88
89 /**
90 * 格式化年月日
91 */
92 public static SimpleDateFormat formatDay = new SimpleDateFormat(YYYYMMDD);
93
94 /**
95 * 格式化年月日
96 */
97 private static SimpleDateFormat format_yyyy_mm_dd = new SimpleDateFormat(
98 YYYY_MM_DD);
99 /**
100 * 格式化年月
101 */
102
103 public static SimpleDateFormat formatMon = new SimpleDateFormat(YYYYMM);
104 /**
105 * 预定义的日期格式:YYYYMMDDHHMMSS
106 */
107 public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
108
109 /**
110 * 格式化年月日
111 */
112 public static final String YYYY_MM_DD_HH_MM = "yyyy/MM/dd HH:mm";
113
114 /**
115 * 按指定的格式sFormat将日期dDate转化为字符串,sFormat的取值在类中定义了常量,也可以自己设置字符串,默认为yyyy-MM-dd
116 *
117 * @param dDate
118 * 待转化的日期
119 * @param sFormat
120 * 格式化指定的格式
121 * @return String 格式为sFormat的日期字符串
122 *
123 */
124 public static String date2String(Date dDate, String sFormat) {
125 if (dDate == null) {
126 return "";
127 } else {
128 if (StringUtil.isEmpty(sFormat)) {
129 sFormat = YYYY_MM_DD;
130 }
131 SimpleDateFormat sdf = new SimpleDateFormat(sFormat);
132 return sdf.format(dDate);
133 }
134 }
135
136 /**
137 *
138 * 将字符串按指定格式转换为java.util.Date类型,format的取值在类中定义了常量,默认格式为yyyy-MM-dd HH:mm:ss
139 *
140 * @param str
141 * 待转化的字符串
142 * @param format
143 * 指定格式
144 * @return Date 返回指定格式为format的日期
145 *
146 */
147 public static Date string2Date(String str, String format) {
148 if (StringUtil.isEmpty(str)) {
149 return null;
150 }
151 Date result = null;
152 if (StringUtil.isEmpty(format)) {
153 return string2Date(str);
154 }
155 try {
156 DateFormat mFormat = new SimpleDateFormat(format);
157 result = mFormat.parse(str);
158 } catch (Exception e) {
159 e.printStackTrace();
160 }
161 return result;
162 }
163
164 /**
165 *
166 * 字符串转换为java.util.Date类型,按字符串的长度来自动设置格式
167 *
168 * @param s
169 * 待转化的字符串
170 * @return Date 按字符串长度设置格式,然后转化为java.util.Date类型
171 *
172 */
173 public static Date string2Date(String s) {
174 if (StringUtil.isEmpty(s)) {
175 return null;
176 }
177 Date result = null;
178 try {
179 DateFormat format = null;
180 if (s.length() > 15) {
181 format = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
182 } else if (s.length() > 8) {
183 format = new SimpleDateFormat(YYYY_MM_DD);
184 } else if (s.length() > 4) {
185 format = new SimpleDateFormat(YYYY_MM);
186 } else {
187 format = new SimpleDateFormat(YYYY);
188 }
189 result = format.parse(s);
190 } catch (Exception e) {
191 e.printStackTrace();
192 }
193 return result;
194 }
195
196 /**
197 * 按指定的格式sFormat格式化日期字符串,并转化为java.sql.Date
198 *
199 * @param str
200 * 待转化的字符串
201 * @param sFormat
202 * 自定的格式
203 * @return java.sql.Date 按格式sFormat格式化日期字符串转化为java.sql.Date类型的对象
204 *
205 */
206 public static java.sql.Date str2SqlDate(String str, String sFormat) {
207 Date date = string2Date(str, sFormat);
208 java.sql.Date sqlDate = new java.sql.Date(date.getTime());
209 return sqlDate;
210 }
211
212 /**
213 * 返回当天所在的年月
214 *
215 * @return String "yyyyMM"
216 *
217 */
218 public static String getCurrentYearMonth() {
219 String res = "";
220 Calendar caldTmp = Calendar.getInstance();
221 caldTmp.setTime(new Date());
222 if (caldTmp.get(Calendar.MONTH) + 1 < 10)
223 res = caldTmp.get(Calendar.YEAR) + "0"
224 + (caldTmp.get(Calendar.MONTH) + 1);
225 else
226 res = caldTmp.get(Calendar.YEAR) + ""
227 + (caldTmp.get(Calendar.MONTH) + 1);
228 return res;
229 }
230
231 /**
232 * 取得当前日期的月份,以MM格式返回.
233 *
234 * @return 当前月份 MM
235 */
236 public static String getCurrentMonth() {
237 return getFormatCurrentTime("MM");
238 }
239
240 /**
241 * 取得当前日期的年份,以yyyy格式返回.
242 *
243 * @return 当年 yyyy
244 */
245 public static String getCurrentYear() {
246 return getFormatCurrentTime("yyyy");
247 }
248
249 /**
250 * 根据给定的格式,返回时间字符串
251 * <p>
252 * 参照DateFormator类,是调用了DateFormator类的date2String方法。
253 *
254 * @param format
255 * 日期格式字符串
256 * @return String 指定格式的日期字符串.
257 */
258 public static String getFormatCurrentTime(String format) {
259 return date2String(new Date(), format);
260 }
261
262 /**
263 *
264 * 添加n月份到一个日期对象,为负数则计算向前n个月
265 *
266 * @param dateInput
267 * 输入日期
268 * @param numberOfMonth
269 * 月数
270 * @return Date 计算后的结果日期
271 *
272 */
273 public static Date addMonths(Date dateInput, int numberOfMonth) {
274 if (dateInput == null) {
275 return null;
276 }
277 java.util.Calendar c = java.util.Calendar.getInstance();
278 c.setTime(dateInput);
279 c.add(java.util.Calendar.MONTH, numberOfMonth);
280 return c.getTime();
281 }
282
283 /**
284 *
285 * 对当前时间,取向前(为负值时向后)多少秒
286 *
287 * @param dInput
288 * 输入时间
289 * @param numberOfSecond
290 * 偏移的秒数
291 * @return Date 结果时间
292 *
293 */
294 public static Date addSecond(Date dInput, int numberOfSecond) {
295 if (dInput == null) {
296 return null;
297 }
298 java.util.Calendar c = java.util.Calendar.getInstance();
299 c.setTime(dInput);
300 c.add(java.util.Calendar.SECOND, numberOfSecond);
301 return c.getTime();
302 }
303
304 /**
305 * 取得前后day天数的日期,day为负数表示以前的日期
306 *
307 * @param date
308 * @param day
309 * @return
310 */
311 public static Date nextDate(Date date, int day) {
312 if (date == null) {
313 return null;
314 }
315 Calendar calendar = Calendar.getInstance();
316 calendar.setTime(date);
317 calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + day);
318 return calendar.getTime();
319 }
320
321 /**
322 *
323 * 验证年份是否为闰年,闰年的条件是: ① 能被4整除,但不能被100整除; ② 能被100整除,又能被400整除。
324 *
325 * @param theYear
326 * 年份,如:2009
327 * @return boolean 是闰年返回true,不是返回false
328 *
329 */
330 public static boolean isLeapYear(int theYear) {
331 return ((theYear % 4 == 0 && theYear % 100 != 0) || (theYear % 400 == 0));
332 }
333
334 /**
335 * 比较两个日期中某个时间单位的间隔数
336 *
337 * @param type
338 * 间隔时间单位类型,取值范围为:yyyy、m、d、h、n、s,对应为年、月、日、时、分、秒
339 * @param fromDate
340 * 起始时间
341 * @param toDate
342 * 终止时间
343 * @return int 按类型用起始时间减去终止时间的值
344 *
345 */
346 public static int dateDiff(String type, Date fromDate, Date toDate) {
347 Calendar fromCalendar = Calendar.getInstance();
348 fromCalendar.setTime(fromDate);
349
350 Calendar toCalendar = Calendar.getInstance();
351 toCalendar.setTime(toDate);
352
353 int fromDay = fromCalendar.get(Calendar.DAY_OF_YEAR);
354 int toDay = toCalendar.get(Calendar.DAY_OF_YEAR);
355 int fromMonth = fromCalendar.get(Calendar.MONTH);
356 int toMonth = toCalendar.get(Calendar.MONTH);
357 int fromYear = fromCalendar.get(Calendar.YEAR);
358 int toYear = toCalendar.get(Calendar.YEAR);
359 int fromHour = fromCalendar.get(Calendar.HOUR_OF_DAY);
360 int toHour = toCalendar.get(Calendar.HOUR_OF_DAY);
361 int fromMinute = fromCalendar.get(Calendar.MINUTE);
362 int toMinute = toCalendar.get(Calendar.MINUTE);
363 int fromSecond = fromCalendar.get(Calendar.SECOND);
364 int toSecond = toCalendar.get(Calendar.SECOND);
365
366 int day = 0;
367 int month = 0;
368 int minute = 0;
369 int second = 0;
370 int hour = 0;
371
372 for (int i = fromYear; i < toYear; i++) {
373 int noOfDay;
374 if (isLeapYear(i))
375 noOfDay = 366;
376 else
377 noOfDay = 365;
378 day = day + noOfDay;
379 hour = hour + (noOfDay * 24);
380 minute = minute + (noOfDay * 24 * 60);
381 second = second + (minute * 60);
382 month = month + 12;
383 }
384 int daydiff = toDay - (fromDay - day);
385 int hourdiff = toHour - (fromHour - hour) + daydiff * 24;
386 int minutediff = toMinute - (fromMinute - minute) + hourdiff * 60;
387 int secdiff = toSecond - (fromSecond - second) + minutediff * 60;
388 if (type.equalsIgnoreCase("yyyy")) {
389 return toYear - fromYear;
390 } else if (type.equalsIgnoreCase("m")) {
391 return toMonth - (fromMonth - month);
392 } else if (type.equalsIgnoreCase("d")) {
393 return daydiff;
394 } else if (type.equalsIgnoreCase("h")) {
395 return hourdiff;
396 } else if (type.equalsIgnoreCase("n")) {
397 return minutediff;
398 } else if (type.equalsIgnoreCase("s")) {
399 return secdiff;
400 } else {
401 return 0;
402 }
403 }
404
405 /**
406 *
407 * 比较两个日期的大小,精确到天
408 *
409 * @param fromDate
410 * 起始时间
411 * @param toDate
412 * 终止时间
413 * @return int 正数表示起始时间比终止时间大,负数表示起始时间比终止时间小
414 *
415 */
416 public static int dateDiffNoTime(Date fromDate, Date toDate) {
417 Calendar fromCalendar = Calendar.getInstance();
418 fromCalendar.setTime(fromDate);
419
420 Calendar toCalendar = Calendar.getInstance();
421 toCalendar.setTime(toDate);
422
423 int fromDay = fromCalendar.get(Calendar.DAY_OF_YEAR);
424 int toDay = toCalendar.get(Calendar.DAY_OF_YEAR);
425 int fromMonth = fromCalendar.get(Calendar.MONTH);
426 int toMonth = toCalendar.get(Calendar.MONTH);
427 int fromYear = fromCalendar.get(Calendar.YEAR);
428 int toYear = toCalendar.get(Calendar.YEAR);
429
430 // Calculate from value
431 int fromDateVal = fromYear * 10000 + fromMonth * 100 + fromDay * 1;
432 // Calculate to value
433 int toDateVal = toYear * 10000 + toMonth * 100 + toDay * 1;
434
435 return (fromDateVal - toDateVal);
436 }
437
438 /**
439 * 取得指定日期对应月的第一天日期
440 *
441 * @param strYYYYMMDD
442 * 日期字符串,日期格式为yyyyMMdd
443 * @return String 处理后的日期字符串,日期格式为yyyy-MM-dd
444 *
445 */
446 public static String getFirstDateOfMonth(String strYYYYMMDD) {
447 Calendar caldTmp = Calendar.getInstance();
448 // 取得该月第一天日期
449 caldTmp.set(DateUtil.getIntYearOfDate(strYYYYMMDD),
450 DateUtil.getIntMonthOfDate(strYYYYMMDD) - 1, 1);
451 return DateUtil.date2String(caldTmp.getTime(), DateUtil.YYYY_MM_DD);
452 }
453
454 /**
455 * 取得指定日期对应月的最后一天日期
456 *
457 * @param strYYYYMMDD
458 * 日期字符串,日期格式为yyyyMMdd
459 * @return String 处理后的日期字符串,日期格式为yyyy-MM-dd
460 *
461 */
462 public static String getLastDateOfMonth(String strYYYYMMDD) {
463 Calendar caldTmp = Calendar.getInstance();
464 // 取得本月第一天日期
465 caldTmp.set(getIntYearOfDate(strYYYYMMDD),
466 (getIntMonthOfDate(strYYYYMMDD) - 1), 1);
467 // 取得该月的下一个月第一天日期
468 caldTmp.add(Calendar.MONTH, 1);
469 // 下月第一天减一天即为本月最后一天
470 caldTmp.add(Calendar.DATE, -1);
471 return date2String(caldTmp.getTime(), YYYY_MM_DD);
472 }
473
474 /**
475 *
476 * 取得指定日期所属week的周一的日期 注:此处返回周一,不是返回周日
477 *
478 * @return String
479 * @param strYYYYMMDD
480 * 日期字符串,格式为yyyyMMdd或yyyy-MM-dd或yyyy/MM/dd
481 * @return String 处理后的日期字符串,格式为yyyy-MM-dd
482 *
483 */
484 public static String getMondayOfThisWeek(String strYYYYMMDD) {
485 Calendar caldTmp = Calendar.getInstance();
486 // 注意:月份的起始值为0而不是1,所以要设置八月时,我们用7而不是8 -> calendar.set(Calendar.MONTH, 7);
487 caldTmp.set(getIntYearOfDate(strYYYYMMDD),
488 getIntMonthOfDate(strYYYYMMDD) - 1,
489 getIntDayOfDate(strYYYYMMDD));
490 int nDayOfWeek = caldTmp.get(Calendar.DAY_OF_WEEK);
491 // System.out.println(" "+strYYYYMMDD+"|| Calendar.DAY_OF_WEEK="+Calendar.DAY_OF_WEEK+"; caldTmp.get(Calendar.DAY_OF_WEEK)="+caldTmp.get(Calendar.DAY_OF_WEEK));
492 caldTmp.add(Calendar.DATE, -(caldTmp.get(Calendar.DAY_OF_WEEK) - 1));
493 // 区别此处不同于西方:周日为每周的第一天,周六为每周最后一天
494 // 周一为每周的第一天,周日为每周最后一天
495 // 此处得到的caldTmp为周日,必须将其做相应修改
496 // 此处得到的caldTmp为周日故需加一天;注意:若指定日期刚好是周日,则需减6天
497 if (nDayOfWeek == 1) {
498 caldTmp.add(Calendar.DATE, -6);
499 } else {
500 caldTmp.add(Calendar.DATE, 1);
501 }
502 return date2String(caldTmp.getTime(), YYYY_MM_DD);
503
504 }
505
506 /**
507 *
508 * 取得指定日期所属week,周日的日期 注:此处返回周日,不是返回周六(按照中国工作周习惯,不同于西方将周六作为周末)
509 *
510 * @param strYYYYMMDD
511 * 日期字符串,格式为yyyyMMdd或yyyy-MM-dd或yyyy/MM/dd
512 * @return String 处理后的日期字符串,格式为yyyy-MM-dd
513 *
514 */
515 public static String getSundayOfThisWeek(String strYYYYMMDD) {
516 String strThisWeekFirstDate = getMondayOfThisWeek(strYYYYMMDD);
517 return date2String(getOffsetDate(strThisWeekFirstDate, 6, "day"),
518 YYYY_MM_DD);
519 }
520
521 /**
522 *
523 * 取给定日期(strYYYYMMDD)所在月的第n(weekIndex)个星期的周一日期
524 *
525 * @param strYYYYMMDD
526 * @param weekIndex
527 * @return String
528 *
529 */
530 public static String getMondayOfWeek(String strYYYYMMDD, int weekIndex) {
531 int nYear = 0;
532 int nMonth = 0;
533 Calendar caldTmp = Calendar.getInstance();
534 nYear = getIntYearOfDate(strYYYYMMDD);
535 nMonth = getIntMonthOfDate(strYYYYMMDD);
536 getIntDayOfDate(strYYYYMMDD);
537 caldTmp.set(nYear, nMonth - 1, 1);
538
539 // 下面为什么这样做,我也不知道,只是这样做就能得到我想要的结果,所以就这样做了……
540 // 得到这个月1日是星期几
541 int dayOfWeek = caldTmp.get(Calendar.DAY_OF_WEEK);
542 // 如果是周日或周一,则按正常情况处理
543 if ((dayOfWeek == Calendar.SUNDAY) || (dayOfWeek == Calendar.MONDAY))
544 caldTmp.set(Calendar.WEEK_OF_MONTH, weekIndex);
545 // 否则,当前周数加一
546 else
547 caldTmp.set(Calendar.WEEK_OF_MONTH, weekIndex + 1);
548
549 // 设置日期为当周的第二天(即周一)
550 caldTmp.set(Calendar.DAY_OF_WEEK, 2);
551
552 String tmpDate = date2String(caldTmp.getTime(), "yyyy-MM-dd");
553
554 // System.out.println("tmpDate: " + tmpDate + " " +
555 // DateDispose.getNumWeekOfMonth(tmpDate));
556
557 return tmpDate;
558 }
559
560 /**
561 *
562 * 获得给定日期所在月共有多少个星期(判断规则:星期一的日期是几月份,则此周属于几月份)
563 *
564 * @param strYYYYMMDD
565 * @return int
566 *
567 */
568 public static int getWeekCountOfMonth(String strYYYYMMDD) {
569 int res = 1;
570 String lastDate;
571 // 取本月的最后一天日期
572 lastDate = getLastDateOfMonth(strYYYYMMDD);
573 // 取本月最后一天是本月的第几个星期
574 res = getWeekIndexOfMonth(lastDate);
575 return res;
576 }
577
578 /**
579 *
580 * 根据年月取得当月的天数
581 *
582 * @param yyyy
583 * 年份
584 * @param mm
585 * 月份
586 * @return int 指定年月的当月天数
587 *
588 */
589 public static int getDaysOfMonth(int yyyy, int mm) {
590 Calendar iCal = Calendar.getInstance();
591 iCal.set(yyyy, mm, 1);
592 iCal.add(Calendar.DATE, -1);
593 return iCal.get(Calendar.DATE);
594 }
595
596 /**
597 *
598 * @return 当前月份有多少天;
599 */
600 public static int getDaysOfCurMonth() {
601 int curyear = new Integer(getCurrentYear()).intValue(); // 当前年份
602 int curMonth = new Integer(getCurrentMonth()).intValue();// 当前月份
603 int mArray[] = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,
604 31 };
605 // 判断闰年的情况 ,2月份有29天;
606 if ((curyear % 400 == 0)
607 || ((curyear % 100 != 0) && (curyear % 4 == 0))) {
608 mArray[1] = 29;
609 }
610 return mArray[curMonth - 1];
611 // 如果要返回下个月的天数,注意处理月份12的情况,防止数组越界;
612 // 如果要返回上个月的天数,注意处理月份1的情况,防止数组越界;
613 }
614
615 /**
616 *
617 * 根据年月日计算是一个星期中的第几天,星期一为第一天
618 *
619 * @param yyyy
620 * @param mm
621 * @param dd
622 * @return int 星期一返回1
623 *
624 */
625 public static int getDayIndexOfWeek(int yyyy, int mm, int dd) {
626 Calendar iCal = Calendar.getInstance();
627 iCal.set(yyyy, mm - 1, dd - 1);
628 return iCal.get(Calendar.DAY_OF_WEEK);
629 }
630
631 /**
632 *
633 * 判断给定日期是所属星期的第几天
634 *
635 * @param strYYYYMMDD
636 * @return int
637 *
638 */
639 public static int getDayIndexOfWeek(String strYYYYMMDD) {
640 int res = 1;
641 int nYear = 0;
642 int nMonth = 0;
643 int nDay = 0;
644 Calendar caldTmp = Calendar.getInstance();
645 nYear = getIntYearOfDate(strYYYYMMDD);
646 nMonth = getIntMonthOfDate(strYYYYMMDD);
647 nDay = getIntDayOfDate(strYYYYMMDD);
648 caldTmp.set(nYear, nMonth - 1, nDay);
649
650 res = caldTmp.get(Calendar.DAY_OF_WEEK) - 1;
651 // 如果为0,说明当天是周日,按中国人的习惯应该是一周的第7天
652 if (res == 0)
653 res = 7;
654 return res;
655 }
656
657 /**
658 *
659 * 取得给定日期为所属月份第几周,日期格式为yyyy-MM-dd
660 *
661 * @param strYYYYMMDD
662 * 日期格式为yyyy-MM-dd
663 * @return int 第一周返回0
664 *
665 */
666 public static int getWeekIndexOfMonth(String strYYYYMMDD) {
667 Calendar caldTmp = Calendar.getInstance();
668 // 按照西方周算出的结果,一周:周日->周六
669 // 中国人习惯,一周:周一->周日
670 caldTmp.setFirstDayOfWeek(Calendar.MONDAY);
671 caldTmp.set(getIntYearOfDate(strYYYYMMDD),
672 getIntMonthOfDate(strYYYYMMDD) - 1,
673 getIntDayOfDate(strYYYYMMDD));
674 int nWeekOfMonth = caldTmp.get(Calendar.WEEK_OF_MONTH);
675 // 注意以下几种情况:
676 // 先判断上月最后一天对应的周末
677 // 1.若当前日期<=上月最后一天对应的周末,则返回0(给定日期属于上月)
678 // 2.若当前日期>=上月最后一天对应的周末
679 // 判断该月一号是否为周日?
680 // 2.1 是周日, return nWeekOfMonth ;
681 // 2.2 否则 return nWeekOfMonth -1 ;
682 // 如:2004-7-1~4都是属上个月最后一周;而2004-8-1为上月最后一周sunday, 2004-8-2属于8月份第一周
683 int nDateDiffNoTime = 0;
684 // 本月一号
685 String strFirstDayOfThisMonth = getFirstDateOfMonth(strYYYYMMDD);
686 // 取得上月最后一周的sunday
687 // 上月最后一天
688 String strLastDateOfPreMonth = date2String(
689 getOffsetDate(strFirstDayOfThisMonth, -1, "day"), YYYY_MM_DD);
690 // 上月最后一天的sunday
691 String strSundayOfLastWeekOfPreMonth = getSundayOfThisWeek(strLastDateOfPreMonth);
692 // 判断给定日期是否小于上月最后一天对应的周末
693 // 注:此处返回的日期字符串可能同传入参数不完全一样2004-08-01 与 2004-8-1;所以必须转为date比较
694 Date dParam = string2Date(strYYYYMMDD, YYYY_MM_DD);
695 Date dSundayOfLastWeekOfPreMonth = string2Date(strSundayOfLastWeekOfPreMonth);
696 nDateDiffNoTime = dateDiffNoTime(dParam, dSundayOfLastWeekOfPreMonth);
697 // System.out.println("-- nWeekOfMonth="+nWeekOfMonth+"; nDateDiffNoTime="+nDateDiffNoTime+"; strYYYYMMDD="+strYYYYMMDD+" ;strSundayOfLastWeekOfPreMonth="+strSundayOfLastWeekOfPreMonth);
698 if (nDateDiffNoTime <= 0) {
699 return 0;
700 } else {
701 // 判断该月一号是否为周日?
702 // 本月1号对应的sunday
703 String strSundayOfFirstDayOfThisMonth = getSundayOfThisWeek(strFirstDayOfThisMonth);
704 // 将给定日期参数规整为标准格式 如:2004-8-1 -> 2004-08-01
705 Date dTmp = string2Date(strYYYYMMDD, YYYY_MM_DD);
706 String strStdYYYYMMDD = date2String(dTmp, YYYY_MM_DD);
707 if (strStdYYYYMMDD
708 .compareToIgnoreCase(strSundayOfFirstDayOfThisMonth) == 0) {
709 // 本月1号为周日
710 return nWeekOfMonth;
711 } else {
712 // 本月1号不为周日
713 // 判断本月一号是否大于上月最后一天对应的周日
714 Date dFistDayOfThisMonth = string2Date(strFirstDayOfThisMonth,
715 YYYY_MM_DD);
716 nDateDiffNoTime = dateDiffNoTime(dFistDayOfThisMonth,
717 dSundayOfLastWeekOfPreMonth);
718 if (nDateDiffNoTime > 0) {
719 return nWeekOfMonth;
720 } else {
721 return nWeekOfMonth - 1;
722 }
723
724 }
725 }
726
727 }
728
729 /**
730 *
731 * 取得日期字符串中的年数值
732 *
733 * @param strYYYYMMDD
734 * @return int 返回年份数值
735 *
736 */
737 public static int getIntYearOfDate(String strYYYYMMDD) {
738 return Integer.parseInt(strYYYYMMDD.substring(0, 4));
739 }
740
741 /**
742 * 取得日期字符串中月份的数值
743 *
744 * @param strYYYYMMDD
745 * @return int
746 *
747 */
748 public static int getIntMonthOfDate(String strYYYYMMDD) {
749 // 确定日期分割符号
750 String strIntervalMark = "";
751 if (strYYYYMMDD.indexOf("/") > 0) {
752 strIntervalMark = "/";
753 } else if (strYYYYMMDD.indexOf("-") > 0) {
754 strIntervalMark = "-";
755 }
756
757 String strMonth = "";
758 String strTmp = "";
759 int nFirstMarkNum = 0;
760 int nSecondMarkNum = 0;
761 nFirstMarkNum = strYYYYMMDD.indexOf(strIntervalMark);
762 strTmp = strYYYYMMDD.substring(nFirstMarkNum + 1);
763 nSecondMarkNum = nFirstMarkNum + strTmp.indexOf(strIntervalMark);
764 if ("".equals(strIntervalMark)) {
765 // YYYYMMDD
766 strMonth = strYYYYMMDD.substring(4, 6);
767 } else {
768 strMonth = strYYYYMMDD.substring(nFirstMarkNum + 1,
769 nSecondMarkNum + 1);
770 }
771 return Integer.parseInt(strMonth);
772 }
773
774 /**
775 * 取得日期字符串中的天数值
776 *
777 * @param strYYYYMMDD
778 * @return int
779 *
780 */
781 public static int getIntDayOfDate(String strYYYYMMDD) {
782 // 确定日期分割符号
783 String strIntervalMark = "";
784 if (strYYYYMMDD.indexOf(" ") > 0)
785 strYYYYMMDD = strYYYYMMDD.substring(0, strYYYYMMDD.indexOf(" "));
786 if (strYYYYMMDD.indexOf("/") > 0) {
787 strIntervalMark = "/";
788 } else if (strYYYYMMDD.indexOf("-") > 0) {
789 strIntervalMark = "-";
790 }
791
792 String strDay = "";
793 int nLastMarkNum = 0;
794 nLastMarkNum = strYYYYMMDD.lastIndexOf(strIntervalMark);
795
796 if (strIntervalMark.compareTo("") == 0) {
797 // YYYYMMDD
798 strDay = strYYYYMMDD.substring(6);
799 } else {
800 strDay = strYYYYMMDD.substring(nLastMarkNum + 1);
801 }
802 return Integer.parseInt(strDay);
803 }
804
805 /**
806 *
807 * 将月份数字(从1到12)转化为英文缩写,月份的前3个字母,小写
808 *
809 * @param mm
810 * 月份
811 * @return String
812 *
813 */
814 public static String getMonthName(int mm) {
815 if (mm == 1) {
816 return "jan";
817 } else if (mm == 2) {
818 return "feb";
819 } else if (mm == 3) {
820 return "mar";
821 } else if (mm == 4) {
822 return "apr";
823 } else if (mm == 5) {
824 return "may";
825 } else if (mm == 6) {
826 return "jun";
827 } else if (mm == 7) {
828 return "jul";
829 } else if (mm == 8) {
830 return "aug";
831 } else if (mm == 9) {
832 return "sep";
833 } else if (mm == 10) {
834 return "oct";
835 } else if (mm == 11) {
836 return "nov";
837 } else if (mm == 12) {
838 return "dec";
839 } else {
840 return null;
841 }
842 }
843
844 /**
845 *
846 * 按升序排序集合中的日期对象
847 *
848 * @param vDate
849 * @return Vector
850 *
851 */
852 public static Vector<Date> sortDateVectorAsc(Vector<?> vDate) {
853 Vector<Date> vSortedDate = new Vector<Date>();
854
855 while (vDate.size() > 0) {
856 Date dDate = getSmallestDate(vDate);
857 if (dDate != null) {
858 vSortedDate.addElement(dDate);
859 }
860 }
861 return vSortedDate;
862 }
863
864 /**
865 *
866 * 取得集合中所有日期类型中的最小日期
867 *
868 * @param vDate
869 * @return Date
870 *
871 */
872 private static Date getSmallestDate(Vector<?> vDate) {
873 int nDeleteIndex = -1;
874 Date dDate = getDateObj(2999, 12, 31);
875 for (int i = 0; i < vDate.size(); i++) {
876 Date dPrevDate = dDate;
877 Date dCurrDate = (Date) vDate.elementAt(i);
878 if (dCurrDate.before(dPrevDate)) {
879 dDate = dCurrDate;
880 nDeleteIndex = i;
881 }
882 }
883 if (nDeleteIndex > -1) {
884 return (Date) vDate.remove(nDeleteIndex);
885 }
886 return null;
887 }
888
889 /**
890 *
891 * 省略掉时间的毫秒,设置millisecond为0
892 *
893 * @param dDate
894 * @return Date
895 *
896 */
897 public static Date trimMillis(Date dDate) {
898 if (dDate == null) {
899 return null;
900 }
901 Calendar cal = Calendar.getInstance();
902 cal.setTime(dDate);
903 cal.set(Calendar.MILLISECOND, 0);
904 return cal.getTime();
905 }
906
907 /**
908 *
909 * 将指定日期偏移一段时间
910 *
911 * @param strYYYYMMDD
912 * 输入日期
913 * @param nOffsetNum
914 * 前/后偏移数量
915 * @param strOffsetUnit
916 * 前/后推周期单位 day,week,month,year,hour
917 * @return Calendar
918 *
919 */
920 public static Date getOffsetDate(String strYYYYMMDD, int nOffsetNum,
921 String strOffsetUnit) {
922 int nYear = 0;
923 int nMonth = 0;
924 int nDay = 0;
925 Calendar caldTmp = Calendar.getInstance();
926 nYear = getIntYearOfDate(strYYYYMMDD);
927 nMonth = getIntMonthOfDate(strYYYYMMDD);
928 nDay = getIntDayOfDate(strYYYYMMDD);
929 caldTmp.set(nYear, nMonth - 1, nDay);
930
931 if ("day".equalsIgnoreCase(strOffsetUnit)) {
932 caldTmp.add(Calendar.DATE, nOffsetNum);
933 } else if ("week".equalsIgnoreCase(strOffsetUnit)) {
934 caldTmp.add(Calendar.DATE, nOffsetNum * 7);
935 } else if ("month".equalsIgnoreCase(strOffsetUnit)) {
936 caldTmp.add(Calendar.MONTH, nOffsetNum);
937 } else if ("year".equalsIgnoreCase(strOffsetUnit)) {
938 caldTmp.add(Calendar.YEAR, nOffsetNum);
939
940 } else if ("hour".equalsIgnoreCase(strOffsetUnit)) {
941 caldTmp.add(Calendar.HOUR, nOffsetNum);
942
943 }
944
945 return caldTmp.getTime();
946 }
947
948 /**
949 * 获得从开始日期到截止日期间所有有效日期的字符数组,参数格式为YYYYmmdd
950 *
951 * @param beginDate
952 * 格式为YYYYmmdd
953 * @param endDate
954 * 格式为YYYYmmdd
955 * @return String[] 格式为"YYYY-MM-DD"的日期字符串数组
956 *
957 */
958 public static String[] getDayList(String beginDate, String endDate) {
959 ArrayList<String> theList = new ArrayList<String>();
960 int beginYear, beginMonth, beginDay, endYear, endMonth, endDay;
961 beginYear = getIntYearOfDate(beginDate);
962 endYear = getIntYearOfDate(endDate);
963 // java中的月份从0-11,所以正常的月份需要 - 1
964 beginMonth = getIntMonthOfDate(beginDate) - 1;
965 endMonth = getIntMonthOfDate(endDate) - 1;
966 beginDay = getIntDayOfDate(beginDate);
967 endDay = getIntDayOfDate(endDate);
968
969 GregorianCalendar bCal = new GregorianCalendar(beginYear, beginMonth,
970 beginDay);
971 GregorianCalendar eCal = new GregorianCalendar(endYear, endMonth,
972 endDay);
973 Date eDate = eCal.getTime();
974 Date bDate = bCal.getTime();
975 String tmpDate;
976 // 对比开始日期与截止日期的大小
977 while (bDate.compareTo(eDate) <= 0) {
978 tmpDate = date2String(bDate, "yyyy-MM-dd");
979 // tmpDate = DateDispose.formatDate(tmpDate.substring(5));
980 // System.out.println(tmpDate);
981 theList.add(tmpDate);
982 // 开始日期加1天
983 bCal.add(Calendar.DATE, 1);
984 bDate = bCal.getTime();
985 }
986 String[] res = new String[theList.size()];
987 res = (String[]) theList.toArray(res);
988 return res;
989 }
990
991 /**
992 * @return String[n] = 获得从开始月份的开始星期到截至月份的截至星期间的所有有效星期的字符数组
993 * @param beginDate
994 * YYYY-MM
995 * @param endDate
996 * YYYY-MM
997 * @param beginWeek
998 * @param endWeek
999 * @return String[] "YYYY-MM|第几周" (年月 与 第几周之间用'|'分隔)
1000 *
1001 */
1002 public static String[] getWeekList(String beginDate, String endDate,
1003 int beginWeek, int endWeek) {
1004 ArrayList<String> theList = new ArrayList<String>();
1005 int beginYear, beginMonth, beginDay, endYear, endMonth, endDay;
1006 int weekCntOfMonth, tmpInt;
1007 beginYear = getIntYearOfDate(beginDate);
1008 endYear = getIntYearOfDate(endDate);
1009 // java中的月份从0-11,所以正常的月份需要 - 1
1010 beginMonth = getIntMonthOfDate(beginDate) - 1;
1011 endMonth = getIntMonthOfDate(endDate) - 1;
1012 // 日期对于判断当前月有几个星期没有关系,所以日期可以为1-31的任何值,我们取一个较中间的值
1013 beginDay = 10;
1014 endDay = 10;
1015
1016 GregorianCalendar bCal = new GregorianCalendar(beginYear, beginMonth,
1017 beginDay);
1018 GregorianCalendar eCal = new GregorianCalendar(endYear, endMonth,
1019 endDay);
1020 Date eDate = eCal.getTime();
1021 Date bDate = bCal.getTime();
1022 String tmpDate, tmpStr;
1023 // 如果开始日期比截至日期小,则不断循环
1024 while (bDate.compareTo(eDate) < 0) {
1025 tmpDate = date2String(bDate, "yyyy-MM-dd");
1026 // 获得当前月共有多少星期
1027 weekCntOfMonth = getWeekCountOfMonth(tmpDate);
1028 // 从本月起始星期 一直 循环到 截至星期
1029 for (tmpInt = beginWeek; tmpInt <= weekCntOfMonth; tmpInt++) {
1030 tmpStr = tmpDate.substring(0, 7) + "|" + tmpInt;
1031 theList.add(tmpStr);
1032 }
1033
1034 // 起始星期回复到第一周
1035 beginWeek = 1;
1036
1037 // 开始日期加1个月
1038 bCal.add(Calendar.MONTH, 1);
1039 bDate = bCal.getTime();
1040 }
1041
1042 // 如果起始日期与截至日期相同
1043 if (bDate.compareTo(eDate) == 0) {
1044 tmpDate = date2String(bDate, "yyyy-MM-dd");
1045 // 获得当前月共有多少星期
1046 weekCntOfMonth = getWeekCountOfMonth(tmpDate);
1047 // 判断当月的星期总数与截至星期参数的大小(截至星期不能大于星期总数)
1048 if (endWeek > weekCntOfMonth)
1049 endWeek = weekCntOfMonth;
1050 for (tmpInt = beginWeek; tmpInt <= endWeek; tmpInt++) {
1051 tmpStr = tmpDate.substring(0, 7) + "|" + tmpInt;
1052 theList.add(tmpStr);
1053 }
1054 }
1055
1056 String[] res = new String[theList.size()];
1057 res = (String[]) theList.toArray(res);
1058 return res;
1059 }
1060
1061 /**
1062 * 获取从开始月份到截至月份间所有有效月份的字符数组
1063 *
1064 * @param beginDate
1065 * @param endDate
1066 * @return String[] 格式为"YYYY-MM"
1067 *
1068 */
1069 public static String[] getMonthList(String beginDate, String endDate) {
1070 ArrayList<String> theList = new ArrayList<String>();
1071 int beginYear, beginMonth, beginDay, endYear, endMonth, endDay;
1072 beginYear = getIntYearOfDate(beginDate);
1073 endYear = getIntYearOfDate(endDate);
1074 // java中的月份从0-11,所以正常的月份需要 - 1
1075 beginMonth = getIntMonthOfDate(beginDate) - 1;
1076 endMonth = getIntMonthOfDate(endDate) - 1;
1077 // 我们只关心年月的大小,但日期也能影响对比,所以我们把日期设置为相同
1078 beginDay = 10;
1079 endDay = 10;
1080
1081 GregorianCalendar bCal = new GregorianCalendar(beginYear, beginMonth,
1082 beginDay);
1083 GregorianCalendar eCal = new GregorianCalendar(endYear, endMonth,
1084 endDay);
1085 Date eDate = eCal.getTime();
1086 Date bDate = bCal.getTime();
1087 String tmpDate, tmpStr;
1088 // 如果开始日期比截至日期小,则不断循环
1089 while (bDate.compareTo(eDate) <= 0) {
1090 tmpDate = date2String(bDate, "yyyy-MM-dd");
1091 tmpStr = tmpDate.substring(0, 7);
1092 // System.out.println(tmpStr);
1093 theList.add(tmpStr);
1094
1095 // 开始日期加1个月
1096 bCal.add(Calendar.MONTH, 1);
1097 bDate = bCal.getTime();
1098 }
1099
1100 String[] res = new String[theList.size()];
1101 res = (String[]) theList.toArray(res);
1102 return res;
1103 }
1104
1105 /**
1106 *
1107 * 获取从开始日期到截至日期间所有有效年份的字符数组
1108 *
1109 * @param beginDate
1110 * @param endDate
1111 * @return String[] 格式为"YYYY"
1112 *
1113 */
1114 public static String[] getYearList(String beginDate, String endDate) {
1115 ArrayList<String> theList = new ArrayList<String>();
1116 int beginYear, beginMonth, beginDay, endYear, endMonth, endDay;
1117 beginYear = getIntYearOfDate(beginDate);
1118 endYear = getIntYearOfDate(endDate);
1119 // 我们只关心年的大小,但月份日期也能影响对比,所以我们把月份日期设置为相同
1120 beginMonth = 10;
1121 endMonth = 10;
1122 beginDay = 10;
1123 endDay = 10;
1124
1125 GregorianCalendar bCal = new GregorianCalendar(beginYear, beginMonth,
1126 beginDay);
1127 GregorianCalendar eCal = new GregorianCalendar(endYear, endMonth,
1128 endDay);
1129 Date eDate = eCal.getTime();
1130 Date bDate = bCal.getTime();
1131 String tmpDate, tmpStr;
1132 // 如果开始日期比截至日期小,则不断循环
1133 while (bDate.compareTo(eDate) <= 0) {
1134 tmpDate = date2String(bDate, "yyyy-MM-dd");
1135 tmpStr = tmpDate.substring(0, 4);
1136 // System.out.println(tmpStr);
1137 theList.add(tmpStr);
1138
1139 // 开始日期加1个年
1140 bCal.add(Calendar.YEAR, 1);
1141 bDate = bCal.getTime();
1142 }
1143
1144 String[] res = new String[theList.size()];
1145 res = (String[]) theList.toArray(res);
1146 return res;
1147 }
1148
1149 /**
1150 * 格式化"MM-dd"或"yyyy-MM"格式的字串,去除字串中月份或日期数字中的"0"
1151 *
1152 * @param date
1153 * "MM-dd"或"yyyy-MM"格式的字串
1154 * @return String
1155 *
1156 */
1157 public static String formatDate(String date) {
1158 String res = "";
1159 if (date == null)
1160 return res;
1161 int year, month, day;
1162 try {
1163 // "MM-dd"格式的字串
1164 if (date.length() == 5) {
1165 // 去除月份和日期前面的"0"
1166 month = Integer.parseInt(date.substring(0, 2));
1167 day = Integer.parseInt(date.substring(3));
1168 res = month + "-" + day;
1169 }
1170 // "yyyy-MM"格式的字串
1171 else if (date.length() == 7) {
1172 year = Integer.parseInt(date.substring(0, 4));
1173 month = Integer.parseInt(date.substring(5));
1174 res = year + "-" + month;
1175 }
1176 } catch (Exception e) {
1177 res = date;
1178 }
1179 return res;
1180 }
1181
1182 /**
1183 * 把日期字符转换为中文含义的日期字符
1184 *
1185 * @param date
1186 * 格式为"yyyy-MM-dd"或"MM-dd"或"yyyy-MM"格式的字串
1187 * @return String "yyyy年MM月dd日"或"MM月dd日"或"yyyy年MM月"格式的字串
1188 *
1189 */
1190 public static String formatDateToCN(String date) {
1191 String res = "";
1192 if (date == null)
1193 return res;
1194 int year, month, day;
1195 try {
1196 // 是"MM-dd"格式的字串
1197 if (date.length() == 5) {
1198 month = Integer.parseInt(date.substring(0, 2));
1199 day = Integer.parseInt(date.substring(3));
1200 res = month + "月" + day + "日";
1201 }
1202 // "yyyy-MM"格式的字串
1203 else if (date.length() == 7) {
1204 year = Integer.parseInt(date.substring(0, 4));
1205 month = Integer.parseInt(date.substring(5, 7));
1206 res = year + "年" + month + "月";
1207 }
1208 // 是"yyyy-MM-dd"格式的字串
1209 else if (date.length() == 10) {
1210 year = Integer.parseInt(date.substring(0, 4));
1211 month = Integer.parseInt(date.substring(5, 7));
1212 day = Integer.parseInt(date.substring(8));
1213 res = year + "年" + month + "月" + day + "日";
1214 } else
1215 res = date;
1216 } catch (Exception e) {
1217 res = date;
1218 }
1219 return res;
1220 }
1221
1222 /**
1223 *
1224 * 通过年月日生成时间对象
1225 *
1226 * @param year
1227 * 年份
1228 * @param month
1229 * 月份
1230 * @param day
1231 * 日期
1232 * @return Date
1233 *
1234 */
1235 public static Date getDateObj(int year, int month, int day) {
1236 int mon = month - 1;
1237 int ye;
1238 Date db;
1239 Calendar rightNow = Calendar.getInstance();
1240 if (year >= 0 && year < 80)
1241 ye = year + 2000;
1242 else if (year > 100)
1243 ye = year;
1244 else
1245 ye = year + 1900;
1246 rightNow.set(Calendar.HOUR_OF_DAY, 0);
1247 rightNow.set(Calendar.MINUTE, 0);
1248 rightNow.set(Calendar.SECOND, 0);
1249 rightNow.set(ye, mon, day);
1250 db = rightNow.getTime();
1251 return db;
1252 }
1253
1254 /**
1255 * 取得指定分隔符分割的年月日的日期对象.
1256 *
1257 * @param argsDate
1258 * 格式为"yyyy-MM-dd"等格式
1259 * @param split
1260 * 时间格式的间隔符,例如“-”,“/”,要和时间一致起来。
1261 * @return 一个java.util.Date()类型的对象
1262 */
1263 public static Date getDateObj(String argsDate, String split) {
1264 String[] temp = argsDate.split(split);
1265 int year = new Integer(temp[0]).intValue();
1266 int month = new Integer(temp[1]).intValue();
1267 int day = new Integer(temp[2]).intValue();
1268 return getDateObj(year, month, day);
1269 }
1270
1271 /**
1272 *
1273 * 原期号i个月之前或者之后的期号值,如200310后5月为200403
1274 *
1275 * @param str
1276 * @param i
1277 * @return String
1278 *
1279 */
1280 public static String addMonth(String str, int i) {
1281 String issue = str; // 原期号格式为:200302
1282 int n_year = Integer.parseInt(issue) / 100;
1283 int n_month = Integer.parseInt(issue) % 100;
1284 int aY = i / 12;
1285 int aM = i % 12;
1286 n_year = n_year + aY;
1287 n_month = n_month + aM;
1288 if (n_month > 12) {
1289 n_year = n_year + 1;
1290 n_month = n_month - 12;
1291 }
1292 if (n_month < 0) {
1293 n_year = n_year - 1;
1294 n_month = 12 + n_month;
1295 }
1296 if (n_month < 10) {
1297 issue = ((Integer.toString(n_year).trim()) + '0' + ((Integer
1298 .toString(n_month).trim())));
1299 } else {
1300 issue = ((Integer.toString(n_year).trim()) + ((Integer
1301 .toString(n_month).trim())));
1302 }
1303
1304 return issue;
1305 }
1306
1307 /**
1308 *
1309 * 根据所给年、月、日,检验是否为合法日期。
1310 *
1311 * @param yyyy
1312 * @param MM
1313 * @param dd
1314 * @return boolean
1315 *
1316 */
1317 public static boolean verifyDate(int yyyy, int MM, int dd) {
1318 boolean flag = false;
1319 if ((MM >= 1) && (MM <= 12) && (dd >= 1) && (dd <= 31)) {
1320 if ((MM == 4) || (MM == 6) || (MM == 9) || (MM == 11)) {
1321 if (dd <= 30) {
1322 flag = true;
1323 }
1324 } else if (MM == 2) {
1325 if (((yyyy % 100 != 0) && (yyyy % 4 == 0)) || (yyyy % 400 == 0)) {
1326 if (dd <= 29) {
1327 flag = true;
1328 }
1329 } else if (dd <= 28) {
1330 flag = true;
1331 }
1332 } else {
1333 flag = true;
1334 }
1335
1336 }
1337 return flag;
1338 }
1339
1340 /**
1341 * 返回当天的日期
1342 *
1343 * @return "YYYY-MM-DD"
1344 */
1345 public static String getToday() {
1346 return DateUtil.date2String(new Date(), DateUtil.YYYY_MM_DD);
1347 }
1348
1349 /**
1350 * 返回前n天账期
1351 *
1352 * @return "YYYY-MM-DD"
1353 */
1354 public static String lastFewDay(int n) {
1355 Calendar cal = Calendar.getInstance();
1356 cal.add(Calendar.DATE, n);
1357 return DateUtil.date2String(cal.getTime(), DateUtil.YYYY_MM_DD);
1358 }
1359
1360 /**
1361 * 返回前n月账期
1362 *
1363 * @return "YYYY-MM"
1364 */
1365 public static String lastFewMon(int n) {
1366 Calendar cal = Calendar.getInstance();
1367 Date today = new Date();
1368 cal.setTime(today);
1369 cal.set(Calendar.MONDAY, cal.get(Calendar.MONDAY) - n);
1370 cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) + n);
1371 return DateUtil.date2String(cal.getTime(), DateUtil.YYYY_MM);
1372 }
1373
1374 /**
1375 * 根据所给的起始时间,间隔天数来计算终止时间
1376 *
1377 * @param startDate
1378 * @param day
1379 * @return 终止时间
1380 */
1381 public static java.sql.Date getStepDay(java.sql.Date date, int step) {
1382 Calendar calendar = Calendar.getInstance();
1383 calendar.setTime(date);
1384 calendar.add(Calendar.DAY_OF_YEAR, step);
1385 return new java.sql.Date(calendar.getTime().getTime());
1386 }
1387
1388 /**
1389 * 得到将date增加指定月数后的date
1390 *
1391 * @param date
1392 * @param intBetween
1393 * @return date加上intBetween月数后的日期
1394 */
1395 public static java.sql.Date getStepMonth(Date date, int intBetween) {
1396 Calendar calo = Calendar.getInstance();
1397 calo.setTime(date);
1398 calo.add(Calendar.MONTH, intBetween);
1399 return new java.sql.Date(calo.getTime().getTime());
1400 }
1401
1402 /**
1403 * 得到将date增加指定年数后的date
1404 *
1405 * @param date
1406 * @param intBetween
1407 * @return date加上intBetween年数后的日期
1408 */
1409 public static java.sql.Date getStepYear(Date date, int intBetween) {
1410 Calendar calo = Calendar.getInstance();
1411 calo.setTime(date);
1412 calo.add(Calendar.YEAR, intBetween);
1413 return new java.sql.Date(calo.getTime().getTime());
1414 }
1415
1416 /*
1417 * 获取昨天的日期
1418 */
1419 public static String getLastDate(String start) {
1420 Calendar cal = Calendar.getInstance();
1421 Date date = null;
1422 try {
1423 date = formatDay.parse(start);
1424 } catch (ParseException e) {
1425 e.printStackTrace();
1426 }
1427 cal.setTime(date);
1428 cal.add(Calendar.DATE, -1);
1429 return formatDay.format(cal.getTime());
1430 }
1431
1432 /*
1433 * 获取前一个月的同一天
1434 */
1435 private static Calendar getDateOfLastMonth(Calendar date) {
1436 Calendar lastDate = (Calendar) date.clone();
1437 lastDate.add(Calendar.MONTH, -1);
1438 return lastDate;
1439 }
1440
1441 /*
1442 * 获取前一个月的同一天
1443 */
1444 public static String getDateOfLastMonth(String dateStr) {
1445 try {
1446 Date date = formatDay.parse(dateStr);
1447 Calendar c = Calendar.getInstance();
1448 c.setTime(date);
1449 Calendar day = getDateOfLastMonth(c);
1450 return formatDay.format(day.getTime());
1451 // return getDateOfLastMonth(c);
1452 } catch (ParseException e) {
1453 throw new IllegalArgumentException(
1454 "Invalid date format(yyyyMMdd): " + dateStr);
1455 }
1456 }
1457
1458 /*
1459 * 取得当前日期的上月
1460 */
1461 public static String getFrontBackStrDate(String strDate) {
1462 if (null == strDate) {
1463 return null;
1464 }
1465 try {
1466 Calendar c = Calendar.getInstance();
1467 c.setTime(formatMon.parse(strDate));
1468 c.add(Calendar.MONTH, -1);
1469 return formatMon.format(c.getTime());
1470 } catch (Exception e) {
1471 e.printStackTrace();
1472 }
1473 return "";
1474 }
1475
1476 /*
1477 * 取得当前日期的上一年同期
1478 */
1479 public static String getFrontYearStrDate(String strDate) {
1480 if (null == strDate) {
1481 return null;
1482 }
1483 try {
1484 Calendar c = Calendar.getInstance();
1485 c.setTime(formatMon.parse(strDate));
1486 c.add(Calendar.YEAR, -1);
1487 return formatMon.format(c.getTime());
1488 } catch (Exception e) {
1489 e.printStackTrace();
1490 }
1491 return "";
1492 }
1493
1494 /**
1495 * 获取两个日期之间所有的日期
1496 *
1497 * @param date1
1498 * @param date2
1499 * @return
1500 */
1501 public static ArrayList<String> getAlldays(String date1, String date2) {
1502 ArrayList<String> L = new ArrayList<String>();
1503 L.add(format_yyyy_mm_dd.format(str2Date(date1).getTime()));
1504 if (date1.equals(date2)) {
1505 // System.out.println("两个日期相等!");
1506 return L;
1507 }
1508
1509 String tmp;
1510 if (date1.compareTo(date2) > 0) { // 确保 date1的日期不晚于date2
1511 tmp = date1;
1512 date1 = date2;
1513 date2 = tmp;
1514 }
1515
1516 tmp = formatDay.format(str2Date(date1).getTime() + 3600 * 24 * 1000);
1517
1518 while (tmp.compareTo(date2) < 0) {
1519 L.add(tmp);
1520 tmp = formatDay.format(str2Date(tmp).getTime() + 3600 * 24 * 1000);
1521 }
1522
1523 // if (num == 0)
1524 // System.out.println("两个日期相邻!");
1525 L.add(formatDay.format(str2Date(date2).getTime()));
1526 return L;
1527 }
1528
1529 /**
1530 * 字符串转换成Date
1531 *
1532 * @param str
1533 * @return
1534 */
1535 private static Date str2Date(String str) {
1536 if (str == null)
1537 return null;
1538
1539 try {
1540 return formatDay.parse(str);
1541 } catch (ParseException e) {
1542 e.printStackTrace();
1543 }
1544 return null;
1545 }
1546
1547 /*
1548 * 获取同期前一月集合
1549 */
1550 public static List<String> getFrontMonthList(List<String> monthList) {
1551 List<String> monthBeforeList = new ArrayList<String>();
1552 for (int i = 1; i < monthList.size(); i++) {
1553 monthBeforeList.add(getDateOfLastMonth(monthList.get(i)));
1554 }
1555 return monthBeforeList;
1556 }
1557
1558 public static List<String> getFrontMonthList2(List<String> monthList) {
1559 List<String> monthBeforeList = new ArrayList<String>();
1560 for (int i = 0; i < monthList.size(); i++) {
1561 monthBeforeList.add(getDateOfLastMonth(monthList.get(i)));
1562 }
1563 return monthBeforeList;
1564 }
1565
1566 /*
1567 * 获取两个日期之间的所有月份
1568 */
1569 public static List<String> getAllMonths(String start, String end) {
1570 Date date1 = null; // 开始日期
1571 Date date2 = null; // 结束日期
1572 try {
1573 date1 = formatMon.parse(start);
1574 date2 = formatMon.parse(end);
1575 } catch (ParseException e) {
1576 e.printStackTrace();
1577 }
1578 Calendar c1 = Calendar.getInstance();
1579 Calendar c2 = Calendar.getInstance();
1580 // 定义集合存放月份
1581 List<String> list = new ArrayList<String>();
1582 // 添加第一个月,即开始时间
1583 list.add(start);
1584 c1.setTime(date1);
1585 c2.setTime(date2);
1586 while (c1.compareTo(c2) < 0) {
1587 c1.add(Calendar.MONTH, 1);// 开始日期加一个月直到等于结束日期为止
1588 Date ss = c1.getTime();
1589 String str = formatMon.format(ss);
1590 list.add(str);
1591 }
1592 return list;
1593 }
1594
1595 /*
1596 * 获取同期前一年集合
1597 */
1598 public static List<String> getFrontYearList(List<String> yearList) {
1599 List<String> yearBeforeList = new ArrayList<String>();
1600 for (int i = 1; i < yearList.size(); i++) {
1601 yearBeforeList.add(getFrontYearStrDate(yearList.get(i)));
1602 }
1603 return yearBeforeList;
1604 }
1605
1606 public static List<String> getFrontYearList2(List<String> yearList) {
1607 List<String> yearBeforeList = new ArrayList<String>();
1608 for (int i = 0; i < yearList.size(); i++) {
1609 yearBeforeList.add(getFrontYearStrDate(yearList.get(i)));
1610 }
1611 return yearBeforeList;
1612 }
1613
1614 public static void main(String args[]) {
1615 /*
1616 public static Date getOffsetDate(String strYYYYMMDD, int nOffsetNum,
1617 String strOffsetUnit) */
1618 /*System.out.println(
1619 formatDay.format(getOffsetDate("20180628", -1,"week")));*/
1620 try {
1621 System.out.println(DateUtil.nextDate(new SimpleDateFormat("yyyy-MM-dd").parse("2018-07-20"),1));
1622 } catch (ParseException e) {
1623 // TODO Auto-generated catch block
1624 e.printStackTrace();
1625 }
1626 }
1627
1628 public static List<String> getBothDateList(String startDate, String endDate) {
1629 List<String> dataList = new ArrayList<String>();
1630 try {
1631 DateFormat dataformat = new SimpleDateFormat(YYYY_MM_DD);
1632 Date startDt = dataformat.parse(startDate);
1633 Date endDt = dataformat.parse(endDate);
1634 Calendar start = Calendar.getInstance();
1635 start.setTime(startDt);
1636 Long startTIme = start.getTimeInMillis();
1637 Calendar end = Calendar.getInstance();
1638 end.setTime(endDt);
1639 Long endTime = end.getTimeInMillis();
1640 Long oneDay = 1000 * 60 * 60 * 24l;
1641 Long time = startTIme;
1642 while (time <= endTime) {
1643 Date retureDt = new Date(time);
1644 dataList.add(dataformat.format(retureDt));
1645 time += oneDay;
1646 }
1647 } catch (Exception e) {
1648 e.printStackTrace();
1649 }
1650 return dataList;
1651 }
1652 }
1653
1654 class StringUtil
1655 {
1656 /**
1657 * 判断字符串是否为空
1658 *
1659 * @param str
1660 * @return boolean
1661 *
1662 */
1663 public static boolean isEmpty(Object str) {
1664 return (str == null || (String.valueOf(str)).trim().length() < 1);
1665 }
1666 }