java 时间工具类 大于_Java常用工具类-时间工具类

1 importjava.text.DateFormat;2 importjava.text.ParseException;3 importjava.text.SimpleDateFormat;4 importjava.util.ArrayList;5 importjava.util.Calendar;6 importjava.util.Date;7 importjava.util.GregorianCalendar;8 importjava.util.List;9 importjava.util.Vector;10

11 /**

12 *13 *

14 * Title: DateUtil.java15 *

16 *

17 * Description: 对日期的处理类,提供一些常用的对日期进行处理的方法18 *

19 *@version1.020 *21 */

22 public classDateUtil {23

24 /**

25 * 预定义的日期格式:yyyy-MM-dd HH:mm:ss26 */

27 public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";28

29 /**

30 * 预定义的日期格式:yyyyMMdd_HHmmss31 */

32 public static final String YYYYMMDD_HHMMSS = "yyyyMMdd_HHmmss";33

34 /**

35 * 预定义的日期格式:yyyy-MM-dd HH:mm36 */

37 public static final String YYYYMMDD_HHMM = "yyyyMMdd HHmm";38

39 /**

40 * 预定义的日期格式:yyyy-MM-dd41 */

42 public static final String YYYY_MM_DD = "yyyy-MM-dd";43

44 /**

45 * 预定义的日期格式:yyyy-MM46 */

47 public static final String YYYY_MM = "yyyy-MM";48

49 /**

50 * 预定义的日期格式:yyyyMMdd51 */

52 public static final String YYYYMMDD = "yyyyMMdd";53

54 /**

55 * 预定义的日期格式:yyyyMM56 */

57 public static final String YYYYMM = "yyyyMM";58

59 /**

60 * 预定义的日期格式:yyyy61 */

62 public static final String YYYY = "yyyy";63

64 /**

65 * 预定义的日期格式:MM-dd66 */

67 public static final String MM_dd = "MM-dd";68

69 /**

70 * 预定义的日期格式:MMdd71 */

72 public static final String MMdd = "MMdd";73

74 /**

75 * 预定义的日期格式:MM76 */

77 public static final String MM = "MM";78

79 /**

80 * 预定义的日期格式:HH:mm81 */

82 public static final String HH_mm = "HH:mm";83

84 /**

85 * 预定义的日期格式:HHmm86 */

87 public static final String HHmm = "HHmm";88

89 /**

90 * 格式化年月日91 */

92 public static SimpleDateFormat formatDay = newSimpleDateFormat(YYYYMMDD);93

94 /**

95 * 格式化年月日96 */

97 private static SimpleDateFormat format_yyyy_mm_dd = newSimpleDateFormat(98 YYYY_MM_DD);99 /**

100 * 格式化年月101 */

102

103 public static SimpleDateFormat formatMon = newSimpleDateFormat(YYYYMM);104 /**

105 * 预定义的日期格式:YYYYMMDDHHMMSS106 */

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-dd116 *117 *@paramdDate118 * 待转化的日期119 *@paramsFormat120 * 格式化指定的格式121 *@returnString 格式为sFormat的日期字符串122 *123 */

124 public staticString 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 = newSimpleDateFormat(sFormat);132 returnsdf.format(dDate);133 }134 }135

136 /**

137 *138 * 将字符串按指定格式转换为java.util.Date类型,format的取值在类中定义了常量,默认格式为yyyy-MM-dd HH:mm:ss139 *140 *@paramstr141 * 待转化的字符串142 *@paramformat143 * 指定格式144 *@returnDate 返回指定格式为format的日期145 *146 */

147 public staticDate string2Date(String str, String format) {148 if(StringUtil.isEmpty(str)) {149 return null;150 }151 Date result = null;152 if(StringUtil.isEmpty(format)) {153 returnstring2Date(str);154 }155 try{156 DateFormat mFormat = newSimpleDateFormat(format);157 result =mFormat.parse(str);158 } catch(Exception e) {159 e.printStackTrace();160 }161 returnresult;162 }163

164 /**

165 *166 * 字符串转换为java.util.Date类型,按字符串的长度来自动设置格式167 *168 *@params169 * 待转化的字符串170 *@returnDate 按字符串长度设置格式,然后转化为java.util.Date类型171 *172 */

173 public staticDate 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 = newSimpleDateFormat(YYYY_MM_DD_HH_MM_SS);182 } else if (s.length() > 8) {183 format = newSimpleDateFormat(YYYY_MM_DD);184 } else if (s.length() > 4) {185 format = newSimpleDateFormat(YYYY_MM);186 } else{187 format = newSimpleDateFormat(YYYY);188 }189 result =format.parse(s);190 } catch(Exception e) {191 e.printStackTrace();192 }193 returnresult;194 }195

196 /**

197 * 按指定的格式sFormat格式化日期字符串,并转化为java.sql.Date198 *199 *@paramstr200 * 待转化的字符串201 *@paramsFormat202 * 自定的格式203 *@returnjava.sql.Date 按格式sFormat格式化日期字符串转化为java.sql.Date类型的对象204 *205 */

206 public staticjava.sql.Date str2SqlDate(String str, String sFormat) {207 Date date =string2Date(str, sFormat);208 java.sql.Date sqlDate = newjava.sql.Date(date.getTime());209 returnsqlDate;210 }211

212 /**

213 * 返回当天所在的年月214 *215 *@returnString "yyyyMM"216 *217 */

218 public staticString getCurrentYearMonth() {219 String res = "";220 Calendar caldTmp =Calendar.getInstance();221 caldTmp.setTime(newDate());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 returnres;229 }230

231 /**

232 * 取得当前日期的月份,以MM格式返回.233 *234 *@return当前月份 MM235 */

236 public staticString getCurrentMonth() {237 return getFormatCurrentTime("MM");238 }239

240 /**

241 * 取得当前日期的年份,以yyyy格式返回.242 *243 *@return当年 yyyy244 */

245 public staticString getCurrentYear() {246 return getFormatCurrentTime("yyyy");247 }248

249 /**

250 * 根据给定的格式,返回时间字符串251 *

252 * 参照DateFormator类,是调用了DateFormator类的date2String方法。253 *254 *@paramformat255 * 日期格式字符串256 *@returnString 指定格式的日期字符串.257 */

258 public staticString getFormatCurrentTime(String format) {259 return date2String(newDate(), format);260 }261

262 /**

263 *264 * 添加n月份到一个日期对象,为负数则计算向前n个月265 *266 *@paramdateInput267 * 输入日期268 *@paramnumberOfMonth269 * 月数270 *@returnDate 计算后的结果日期271 *272 */

273 public static Date addMonths(Date dateInput, intnumberOfMonth) {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 returnc.getTime();281 }282

283 /**

284 *285 * 对当前时间,取向前(为负值时向后)多少秒286 *287 *@paramdInput288 * 输入时间289 *@paramnumberOfSecond290 * 偏移的秒数291 *@returnDate 结果时间292 *293 */

294 public static Date addSecond(Date dInput, intnumberOfSecond) {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 returnc.getTime();302 }303

304 /**

305 * 取得前后day天数的日期,day为负数表示以前的日期306 *307 *@paramdate308 *@paramday309 *@return

310 */

311 public static Date nextDate(Date date, intday) {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 returncalendar.getTime();319 }320

321 /**

322 *323 * 验证年份是否为闰年,闰年的条件是: ① 能被4整除,但不能被100整除; ② 能被100整除,又能被400整除。324 *325 *@paramtheYear326 * 年份,如:2009327 *@returnboolean 是闰年返回true,不是返回false328 *329 */

330 public static boolean isLeapYear(inttheYear) {331 return ((theYear % 4 == 0 && theYear % 100 != 0) || (theYear % 400 == 0));332 }333

334 /**

335 * 比较两个日期中某个时间单位的间隔数336 *337 *@paramtype338 * 间隔时间单位类型,取值范围为:yyyy、m、d、h、n、s,对应为年、月、日、时、分、秒339 *@paramfromDate340 * 起始时间341 *@paramtoDate342 * 终止时间343 *@returnint 按类型用起始时间减去终止时间的值344 *345 */

346 public static intdateDiff(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 intnoOfDay;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 returndaydiff;394 } else if (type.equalsIgnoreCase("h")) {395 returnhourdiff;396 } else if (type.equalsIgnoreCase("n")) {397 returnminutediff;398 } else if (type.equalsIgnoreCase("s")) {399 returnsecdiff;400 } else{401 return 0;402 }403 }404

405 /**

406 *407 * 比较两个日期的大小,精确到天408 *409 *@paramfromDate410 * 起始时间411 *@paramtoDate412 * 终止时间413 *@returnint 正数表示起始时间比终止时间大,负数表示起始时间比终止时间小414 *415 */

416 public static intdateDiffNoTime(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 *@paramstrYYYYMMDD442 * 日期字符串,日期格式为yyyyMMdd443 *@returnString 处理后的日期字符串,日期格式为yyyy-MM-dd444 *445 */

446 public staticString getFirstDateOfMonth(String strYYYYMMDD) {447 Calendar caldTmp =Calendar.getInstance();448 //取得该月第一天日期

449 caldTmp.set(DateUtil.getIntYearOfDate(strYYYYMMDD),450 DateUtil.getIntMonthOfDate(strYYYYMMDD) - 1, 1);451 returnDateUtil.date2String(caldTmp.getTime(), DateUtil.YYYY_MM_DD);452 }453

454 /**

455 * 取得指定日期对应月的最后一天日期456 *457 *@paramstrYYYYMMDD458 * 日期字符串,日期格式为yyyyMMdd459 *@returnString 处理后的日期字符串,日期格式为yyyy-MM-dd460 *461 */

462 public staticString 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 returndate2String(caldTmp.getTime(), YYYY_MM_DD);472 }473

474 /**

475 *476 * 取得指定日期所属week的周一的日期 注:此处返回周一,不是返回周日477 *478 *@returnString479 *@paramstrYYYYMMDD480 * 日期字符串,格式为yyyyMMdd或yyyy-MM-dd或yyyy/MM/dd481 *@returnString 处理后的日期字符串,格式为yyyy-MM-dd482 *483 */

484 public staticString 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 returndate2String(caldTmp.getTime(), YYYY_MM_DD);503

504 }505

506 /**

507 *508 * 取得指定日期所属week,周日的日期 注:此处返回周日,不是返回周六(按照中国工作周习惯,不同于西方将周六作为周末)509 *510 *@paramstrYYYYMMDD511 * 日期字符串,格式为yyyyMMdd或yyyy-MM-dd或yyyy/MM/dd512 *@returnString 处理后的日期字符串,格式为yyyy-MM-dd513 *514 */

515 public staticString 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 *@paramstrYYYYMMDD526 *@paramweekIndex527 *@returnString528 *529 */

530 public static String getMondayOfWeek(String strYYYYMMDD, intweekIndex) {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 returntmpDate;558 }559

560 /**

561 *562 * 获得给定日期所在月共有多少个星期(判断规则:星期一的日期是几月份,则此周属于几月份)563 *564 *@paramstrYYYYMMDD565 *@returnint566 *567 */

568 public static intgetWeekCountOfMonth(String strYYYYMMDD) {569 int res = 1;570 String lastDate;571 //取本月的最后一天日期

572 lastDate =getLastDateOfMonth(strYYYYMMDD);573 //取本月最后一天是本月的第几个星期

574 res =getWeekIndexOfMonth(lastDate);575 returnres;576 }577

578 /**

579 *580 * 根据年月取得当月的天数581 *582 *@paramyyyy583 * 年份584 *@parammm585 * 月份586 *@returnint 指定年月的当月天数587 *588 */

589 public static int getDaysOfMonth(int yyyy, intmm) {590 Calendar iCal =Calendar.getInstance();591 iCal.set(yyyy, mm, 1);592 iCal.add(Calendar.DATE, -1);593 returniCal.get(Calendar.DATE);594 }595

596 /**

597 *598 *@return当前月份有多少天;599 */

600 public static intgetDaysOfCurMonth() {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 *@paramyyyy620 *@parammm621 *@paramdd622 *@returnint 星期一返回1623 *624 */

625 public static int getDayIndexOfWeek(int yyyy, int mm, intdd) {626 Calendar iCal =Calendar.getInstance();627 iCal.set(yyyy, mm - 1, dd - 1);628 returniCal.get(Calendar.DAY_OF_WEEK);629 }630

631 /**

632 *633 * 判断给定日期是所属星期的第几天634 *635 *@paramstrYYYYMMDD636 *@returnint637 *638 */

639 public static intgetDayIndexOfWeek(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 returnres;655 }656

657 /**

658 *659 * 取得给定日期为所属月份第几周,日期格式为yyyy-MM-dd660 *661 *@paramstrYYYYMMDD662 * 日期格式为yyyy-MM-dd663 *@returnint 第一周返回0664 *665 */

666 public static intgetWeekIndexOfMonth(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 //取得上月最后一周的sunday687 //上月最后一天

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(strStdYYYYMMDD708 .compareToIgnoreCase(strSundayOfFirstDayOfThisMonth) == 0) {709 //本月1号为周日

710 returnnWeekOfMonth;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 returnnWeekOfMonth;720 } else{721 return nWeekOfMonth - 1;722 }723

724 }725 }726

727 }728

729 /**

730 *731 * 取得日期字符串中的年数值732 *733 *@paramstrYYYYMMDD734 *@returnint 返回年份数值735 *736 */

737 public static intgetIntYearOfDate(String strYYYYMMDD) {738 return Integer.parseInt(strYYYYMMDD.substring(0, 4));739 }740

741 /**

742 * 取得日期字符串中月份的数值743 *744 *@paramstrYYYYMMDD745 *@returnint746 *747 */

748 public static intgetIntMonthOfDate(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 returnInteger.parseInt(strMonth);772 }773

774 /**

775 * 取得日期字符串中的天数值776 *777 *@paramstrYYYYMMDD778 *@returnint779 *780 */

781 public static intgetIntDayOfDate(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 returnInteger.parseInt(strDay);803 }804

805 /**

806 *807 * 将月份数字(从1到12)转化为英文缩写,月份的前3个字母,小写808 *809 *@parammm810 * 月份811 *@returnString812 *813 */

814 public static String getMonthName(intmm) {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 *@paramvDate849 *@returnVector850 *851 */

852 public static Vector sortDateVectorAsc(Vector>vDate) {853 Vector vSortedDate = new Vector();854

855 while (vDate.size() > 0) {856 Date dDate =getSmallestDate(vDate);857 if (dDate != null) {858 vSortedDate.addElement(dDate);859 }860 }861 returnvSortedDate;862 }863

864 /**

865 *866 * 取得集合中所有日期类型中的最小日期867 *868 *@paramvDate869 *@returnDate870 *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为0892 *893 *@paramdDate894 *@returnDate895 *896 */

897 public staticDate 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 returncal.getTime();905 }906

907 /**

908 *909 * 将指定日期偏移一段时间910 *911 *@paramstrYYYYMMDD912 * 输入日期913 *@paramnOffsetNum914 * 前/后偏移数量915 *@paramstrOffsetUnit916 * 前/后推周期单位 day,week,month,year,hour917 *@returnCalendar918 *919 */

920 public static Date getOffsetDate(String strYYYYMMDD, intnOffsetNum,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 returncaldTmp.getTime();946 }947

948 /**

949 * 获得从开始日期到截止日期间所有有效日期的字符数组,参数格式为YYYYmmdd950 *951 *@parambeginDate952 * 格式为YYYYmmdd953 *@paramendDate954 * 格式为YYYYmmdd955 *@returnString[] 格式为"YYYY-MM-DD"的日期字符串数组956 *957 */

958 public staticString[] getDayList(String beginDate, String endDate) {959 ArrayList theList = new ArrayList();960 intbeginYear, 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 = newGregorianCalendar(beginYear, beginMonth,970 beginDay);971 GregorianCalendar eCal = newGregorianCalendar(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 = newString[theList.size()];987 res =(String[]) theList.toArray(res);988 returnres;989 }990

991 /**

992 *@returnString[n] = 获得从开始月份的开始星期到截至月份的截至星期间的所有有效星期的字符数组993 *@parambeginDate994 * YYYY-MM995 *@paramendDate996 * YYYY-MM997 *@parambeginWeek998 *@paramendWeek999 *@returnString[] "YYYY-MM|第几周" (年月 与 第几周之间用'|'分隔)1000 *1001 */

1002 public staticString[] getWeekList(String beginDate, String endDate,1003 int beginWeek, intendWeek) {1004 ArrayList theList = new ArrayList();1005 intbeginYear, beginMonth, beginDay, endYear, endMonth, endDay;1006 intweekCntOfMonth, 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 = newGregorianCalendar(beginYear, beginMonth,1017 beginDay);1018 GregorianCalendar eCal = newGregorianCalendar(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 = newString[theList.size()];1057 res =(String[]) theList.toArray(res);1058 returnres;1059 }1060

1061 /**

1062 * 获取从开始月份到截至月份间所有有效月份的字符数组1063 *1064 *@parambeginDate1065 *@paramendDate1066 *@returnString[] 格式为"YYYY-MM"1067 *1068 */

1069 public staticString[] getMonthList(String beginDate, String endDate) {1070 ArrayList theList = new ArrayList();1071 intbeginYear, 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 = newGregorianCalendar(beginYear, beginMonth,1082 beginDay);1083 GregorianCalendar eCal = newGregorianCalendar(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 = newString[theList.size()];1101 res =(String[]) theList.toArray(res);1102 returnres;1103 }1104

1105 /**

1106 *1107 * 获取从开始日期到截至日期间所有有效年份的字符数组1108 *1109 *@parambeginDate1110 *@paramendDate1111 *@returnString[] 格式为"YYYY"1112 *1113 */

1114 public staticString[] getYearList(String beginDate, String endDate) {1115 ArrayList theList = new ArrayList();1116 intbeginYear, 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 = newGregorianCalendar(beginYear, beginMonth,1126 beginDay);1127 GregorianCalendar eCal = newGregorianCalendar(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 = newString[theList.size()];1145 res =(String[]) theList.toArray(res);1146 returnres;1147 }1148

1149 /**

1150 * 格式化"MM-dd"或"yyyy-MM"格式的字串,去除字串中月份或日期数字中的"0"1151 *1152 *@paramdate1153 * "MM-dd"或"yyyy-MM"格式的字串1154 *@returnString1155 *1156 */

1157 public staticString formatDate(String date) {1158 String res = "";1159 if (date == null)1160 returnres;1161 intyear, 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 returnres;1180 }1181

1182 /**

1183 * 把日期字符转换为中文含义的日期字符1184 *1185 *@paramdate1186 * 格式为"yyyy-MM-dd"或"MM-dd"或"yyyy-MM"格式的字串1187 *@returnString "yyyy年MM月dd日"或"MM月dd日"或"yyyy年MM月"格式的字串1188 *1189 */

1190 public staticString formatDateToCN(String date) {1191 String res = "";1192 if (date == null)1193 returnres;1194 intyear, 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 returnres;1220 }1221

1222 /**

1223 *1224 * 通过年月日生成时间对象1225 *1226 *@paramyear1227 * 年份1228 *@parammonth1229 * 月份1230 *@paramday1231 * 日期1232 *@returnDate1233 *1234 */

1235 public static Date getDateObj(int year, int month, intday) {1236 int mon = month - 1;1237 intye;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 returndb;1252 }1253

1254 /**

1255 * 取得指定分隔符分割的年月日的日期对象.1256 *1257 *@paramargsDate1258 * 格式为"yyyy-MM-dd"等格式1259 *@paramsplit1260 * 时间格式的间隔符,例如“-”,“/”,要和时间一致起来。1261 *@return一个java.util.Date()类型的对象1262 */

1263 public staticDate 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 returngetDateObj(year, month, day);1269 }1270

1271 /**

1272 *1273 * 原期号i个月之前或者之后的期号值,如200310后5月为2004031274 *1275 *@paramstr1276 *@parami1277 *@returnString1278 *1279 */

1280 public static String addMonth(String str, inti) {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' +((Integer1298 .toString(n_month).trim())));1299 } else{1300 issue = ((Integer.toString(n_year).trim()) +((Integer1301 .toString(n_month).trim())));1302 }1303

1304 returnissue;1305 }1306

1307 /**

1308 *1309 * 根据所给年、月、日,检验是否为合法日期。1310 *1311 *@paramyyyy1312 *@paramMM1313 *@paramdd1314 *@returnboolean1315 *1316 */

1317 public static boolean verifyDate(int yyyy, int MM, intdd) {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 returnflag;1338 }1339

1340 /**

1341 * 返回当天的日期1342 *1343 *@return"YYYY-MM-DD"1344 */

1345 public staticString getToday() {1346 return DateUtil.date2String(newDate(), DateUtil.YYYY_MM_DD);1347 }1348

1349 /**

1350 * 返回前n天账期1351 *1352 *@return"YYYY-MM-DD"1353 */

1354 public static String lastFewDay(intn) {1355 Calendar cal =Calendar.getInstance();1356 cal.add(Calendar.DATE, n);1357 returnDateUtil.date2String(cal.getTime(), DateUtil.YYYY_MM_DD);1358 }1359

1360 /**

1361 * 返回前n月账期1362 *1363 *@return"YYYY-MM"1364 */

1365 public static String lastFewMon(intn) {1366 Calendar cal =Calendar.getInstance();1367 Date today = newDate();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 returnDateUtil.date2String(cal.getTime(), DateUtil.YYYY_MM);1372 }1373

1374 /**

1375 * 根据所给的起始时间,间隔天数来计算终止时间1376 *1377 *@paramstartDate1378 *@paramday1379 *@return终止时间1380 */

1381 public static java.sql.Date getStepDay(java.sql.Date date, intstep) {1382 Calendar calendar =Calendar.getInstance();1383 calendar.setTime(date);1384 calendar.add(Calendar.DAY_OF_YEAR, step);1385 return newjava.sql.Date(calendar.getTime().getTime());1386 }1387

1388 /**

1389 * 得到将date增加指定月数后的date1390 *1391 *@paramdate1392 *@paramintBetween1393 *@returndate加上intBetween月数后的日期1394 */

1395 public static java.sql.Date getStepMonth(Date date, intintBetween) {1396 Calendar calo =Calendar.getInstance();1397 calo.setTime(date);1398 calo.add(Calendar.MONTH, intBetween);1399 return newjava.sql.Date(calo.getTime().getTime());1400 }1401

1402 /**

1403 * 得到将date增加指定年数后的date1404 *1405 *@paramdate1406 *@paramintBetween1407 *@returndate加上intBetween年数后的日期1408 */

1409 public static java.sql.Date getStepYear(Date date, intintBetween) {1410 Calendar calo =Calendar.getInstance();1411 calo.setTime(date);1412 calo.add(Calendar.YEAR, intBetween);1413 return newjava.sql.Date(calo.getTime().getTime());1414 }1415

1416 /*

1417 * 获取昨天的日期1418 */

1419 public staticString 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 returnformatDay.format(cal.getTime());1430 }1431

1432 /*

1433 * 获取前一个月的同一天1434 */

1435 private staticCalendar getDateOfLastMonth(Calendar date) {1436 Calendar lastDate =(Calendar) date.clone();1437 lastDate.add(Calendar.MONTH, -1);1438 returnlastDate;1439 }1440

1441 /*

1442 * 获取前一个月的同一天1443 */

1444 public staticString 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 returnformatDay.format(day.getTime());1451 //return getDateOfLastMonth(c);

1452 } catch(ParseException e) {1453 throw newIllegalArgumentException(1454 "Invalid date format(yyyyMMdd): " +dateStr);1455 }1456 }1457

1458 /*

1459 * 取得当前日期的上月1460 */

1461 public staticString 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 returnformatMon.format(c.getTime());1470 } catch(Exception e) {1471 e.printStackTrace();1472 }1473 return "";1474 }1475

1476 /*

1477 * 取得当前日期的上一年同期1478 */

1479 public staticString 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 returnformatMon.format(c.getTime());1488 } catch(Exception e) {1489 e.printStackTrace();1490 }1491 return "";1492 }1493

1494 /**

1495 * 获取两个日期之间所有的日期1496 *1497 *@paramdate11498 *@paramdate21499 *@return

1500 */

1501 public static ArrayListgetAlldays(String date1, String date2) {1502 ArrayList L = new ArrayList();1503 L.add(format_yyyy_mm_dd.format(str2Date(date1).getTime()));1504 if(date1.equals(date2)) {1505 //System.out.println("两个日期相等!");

1506 returnL;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 returnL;1527 }1528

1529 /**

1530 * 字符串转换成Date1531 *1532 *@paramstr1533 *@return

1534 */

1535 private staticDate str2Date(String str) {1536 if (str == null)1537 return null;1538

1539 try{1540 returnformatDay.parse(str);1541 } catch(ParseException e) {1542 e.printStackTrace();1543 }1544 return null;1545 }1546

1547 /*

1548 * 获取同期前一月集合1549 */

1550 public static List getFrontMonthList(ListmonthList) {1551 List monthBeforeList = new ArrayList();1552 for (int i = 1; i < monthList.size(); i++) {1553 monthBeforeList.add(getDateOfLastMonth(monthList.get(i)));1554 }1555 returnmonthBeforeList;1556 }1557

1558 public static List getFrontMonthList2(ListmonthList) {1559 List monthBeforeList = new ArrayList();1560 for (int i = 0; i < monthList.size(); i++) {1561 monthBeforeList.add(getDateOfLastMonth(monthList.get(i)));1562 }1563 returnmonthBeforeList;1564 }1565

1566 /*

1567 * 获取两个日期之间的所有月份1568 */

1569 public static ListgetAllMonths(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 list = new ArrayList();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 returnlist;1593 }1594

1595 /*

1596 * 获取同期前一年集合1597 */

1598 public static List getFrontYearList(ListyearList) {1599 List yearBeforeList = new ArrayList();1600 for (int i = 1; i < yearList.size(); i++) {1601 yearBeforeList.add(getFrontYearStrDate(yearList.get(i)));1602 }1603 returnyearBeforeList;1604 }1605

1606 public static List getFrontYearList2(ListyearList) {1607 List yearBeforeList = new ArrayList();1608 for (int i = 0; i < yearList.size(); i++) {1609 yearBeforeList.add(getFrontYearStrDate(yearList.get(i)));1610 }1611 returnyearBeforeList;1612 }1613

1614 public static voidmain(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 ListgetBothDateList(String startDate, String endDate) {1629 List dataList = new ArrayList();1630 try{1631 DateFormat dataformat = newSimpleDateFormat(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 = newDate(time);1644 dataList.add(dataformat.format(retureDt));1645 time +=oneDay;1646 }1647 } catch(Exception e) {1648 e.printStackTrace();1649 }1650 returndataList;1651 }1652 }1653

1654 classStringUtil1655 {1656 /**

1657 * 判断字符串是否为空1658 *1659 *@paramstr1660 *@returnboolean1661 *1662 */

1663 public static booleanisEmpty(Object str) {1664 return (str == null || (String.valueOf(str)).trim().length() < 1);1665 }1666 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值