public String calLeaveDays(Date startTime, Date endTime, Integer vacateType) {// Integer vacateType;//请假类型 4是产假if (vacateType==null) vacateType=6;int leaveDays = 0;//从startTime开始循环,若该日期不是节假日或者不是周六日则请假天数+1Date flag = DateUtil.beginOfDay(startTime);//设置循环开始日期Date flagEnd = DateUtil.beginOfDay(endTime);//设置循环结束日期Calendar cal = Calendar.getInstance();Calendar calStart = Calendar.getInstance();calStart.setTime(startTime);Calendar calEnd = Calendar.getInstance();calEnd.setTime(endTime);int startHours = startTime.getHours();int endHours = endTime.getHours();int hour = 0;//从数据库得到节假日的起始日期和终止日期List<HolidayVO> maps = null;try {maps = vacateDao.getHoliday();//maps用于保存符合条件的所有节假日的起始日期和终止日期,如startDate:-07-13,endDate:-07-14} catch (Exception e) {e.printStackTrace();}int startWeek = calStart.get(Calendar.DAY_OF_WEEK) - 1;//===================校验请假的开始与结束日期是否合法=============================if (vacateType!=4) {//不是产假才去限制开始和结束时间//判断开始和结束的日期是否合法if (maps != null && maps.size() > 0) {for (HolidayVO map : maps) {if ((DateUtil.beginOfDay(startTime).compareTo(map.getBeginDate()) > -1 && DateUtil.beginOfDay(startTime).compareTo(map.getEndDate()) < 1)) {return "-2天0小时";//开始时间是节假日,请重新选择日期!}}} else if (startWeek == 0) {return "-1天0小时";//开始时间是周末,请重新选择!}int endWeek = calEnd.get(Calendar.DAY_OF_WEEK) - 1;//结束日期if (maps != null && maps.size() > 0) {for (HolidayVO map : maps) {if ((DateUtil.beginOfDay(endTime).compareTo(map.getBeginDate()) > -1 && DateUtil.beginOfDay(endTime).compareTo(map.getEndDate()) <= 0)) {return "-4天0小时";//结束时间是节假日,请重新选择!}}} else if (endWeek == 0) {return "-3天0小时";//结束时间是周末,请重新选择!}}//========================一天之内=========================if (DateUtil.beginOfDay(startTime).compareTo(DateUtil.beginOfDay(endTime)) == 0) {hour = endHours - startHours;if (startHours <= 12 && endHours >= 13) --hour;if (hour == 8) {++leaveDays;hour = 0;}return leaveDays + "天" + hour + "小时";}if (vacateType==4) {while (pareTo(endTime) != 1) {cal.setTime(flag);leaveDays = leaveDays + 1;cal.add(Calendar.DAY_OF_MONTH, +1);flag = cal.getTime();}} else {int week;outer:while (pareTo(endTime) != 1) {cal.setTime(flag);// //判断是否为周六日// week = cal.get(Calendar.DAY_OF_WEEK) - 1;// if (week == 0) {//0为周日////跳出循环进入下一个日期//cal.add(Calendar.DAY_OF_MONTH, +1);//flag = cal.getTime();//continue;// } else {//判断是否为节假日if (maps != null || !maps.isEmpty()) {inner:for (HolidayVO map : maps) {if (pareTo(map.getBeginDate()) > -1 && pareTo(map.getEndDate()) < 1) {//跳出循环进入下一个日期cal.add(Calendar.DAY_OF_MONTH, +1);flag = cal.getTime();continue outer;}}}// }leaveDays = leaveDays + 1;//日期往后加一天cal.add(Calendar.DAY_OF_MONTH, +1);flag = cal.getTime();}}if (startHours < 8) startHours = 8;if (startHours > 17) startHours = 17;if (endHours > 17) endHours = 17;if (endHours < 8) endHours = 8;//=================请假日期不在一天===================//开始时间是8点之前if (pareTo(endTime) < 0 && startTime.getHours() <= 8 && endTime.getHours() > 8 && endTime.getHours() < 17) {leaveDays--;startHours = 8;}//开始时间是8点到17点之间if (pareTo(endTime) < 0 && startTime.getHours() > 8 && startTime.getHours() < 17 && endTime.getHours() <= 8) {--leaveDays;--leaveDays;endHours = 17;}if (pareTo(endTime) < 0 && startTime.getHours() > 8 && startTime.getHours() < 17 && endTime.getHours() > 8 && endTime.getHours() < 17) {--leaveDays;startHours = 8;}if (pareTo(endTime) < 0 && startTime.getHours() > 8 && startTime.getHours() < 17 && endTime.getHours() >= 17) {startHours = 8;--leaveDays;}//开始时间是17点之后if (pareTo(endTime) < 0 && startTime.getHours() >= 17 && endTime.getHours() > 8 && endTime.getHours() < 17) {--leaveDays;--leaveDays;startHours = 8;}hour = endHours - startHours;//中午休息时间if (startHours <= 12 && endHours >= 13) --hour;//整数天if (pareTo(endTime) < 0 && startTime.getHours() <= 8 && endTime.getHours() >= 17) leaveDays--;if (pareTo(endTime) < 0 && startTime.getHours() <= 8 && endTime.getHours() <= 8) {--leaveDays;hour = 0;}if (pareTo(endTime) < 0 && startTime.getHours() >= 17 && endTime.getHours() <= 8) {--leaveDays;--leaveDays;hour = 0;}if (pareTo(endTime) < 0 && startTime.getHours() >= 17 && endTime.getHours() >= 17) {leaveDays--;hour = 0;}if (hour == 8) {leaveDays++;hour = 0;}return leaveDays + "天" + hour + "小时";
优化之后
/*** 出去节假日,计算两个时间之间的天数** @param startTime* @param endTime* @return*/public String calLeaveDays(Date startTime, Date endTime, Integer vacateType) {// Integer vacateType;//请假类型 4是产假if (vacateType==null) vacateType=6;int leaveDays = 0;//从startTime开始循环,若该日期不是节假日或者不是周六日则请假天数+1Date flag = DateUtil.beginOfDay(startTime);//设置循环开始日期Date flagEnd = DateUtil.beginOfDay(endTime);//设置循环结束日期Calendar cal = Calendar.getInstance();Calendar calStart = Calendar.getInstance();calStart.setTime(startTime);Calendar calEnd = Calendar.getInstance();calEnd.setTime(endTime);int startHours = startTime.getHours();int endHours = endTime.getHours();int hour = 0;//从数据库得到节假日的起始日期和终止日期List<HolidayVO> maps = null;try {maps = vacateDao.getHoliday();//maps用于保存符合条件的所有节假日的起始日期和终止日期,如startDate:-07-13,endDate:-07-14} catch (Exception e) {e.printStackTrace();}int startWeek = calStart.get(Calendar.DAY_OF_WEEK) - 1;//===================校验请假的开始与结束日期是否合法=============================if (vacateType!=4) {//不是产假才去限制开始和结束时间//判断开始和结束的日期是否合法if (maps != null && maps.size() > 0) {for (HolidayVO map : maps) {if ((DateUtil.beginOfDay(startTime).compareTo(map.getBeginDate()) > -1 && DateUtil.beginOfDay(startTime).compareTo(map.getEndDate()) < 1)) {return "-2天0小时";//开始时间是节假日,请重新选择日期!}}} else if (startWeek == 0) {return "-1天0小时";//开始时间是周末,请重新选择!}int endWeek = calEnd.get(Calendar.DAY_OF_WEEK) - 1;//结束日期if (maps != null && maps.size() > 0) {for (HolidayVO map : maps) {if ((DateUtil.beginOfDay(endTime).compareTo(map.getBeginDate()) > -1 && DateUtil.beginOfDay(endTime).compareTo(map.getEndDate()) <= 0)) {return "-4天0小时";//结束时间是节假日,请重新选择!}}} else if (endWeek == 0) {return "-3天0小时";//结束时间是周末,请重新选择!}}//========================一天之内=========================if (DateUtil.beginOfDay(startTime).compareTo(DateUtil.beginOfDay(endTime)) == 0) {hour = endHours - startHours;if (startHours <= 12 && endHours >= 13) --hour;if (hour == 8) {++leaveDays;hour = 0;}return leaveDays + "天" + hour + "小时";}//======================多天时间计算=============================cal.setTime(flagEnd);cal.add(Calendar.DAY_OF_MONTH,-1);flagEnd=cal.getTime();//设置结束日期cal.setTime(flag);cal.add(Calendar.DAY_OF_MONTH, +1);flag = cal.getTime();//开始日期if (vacateType==4) {while (pareTo(flagEnd) != 1) {cal.setTime(flag);leaveDays = leaveDays + 1;cal.add(Calendar.DAY_OF_MONTH, +1);flag = cal.getTime();}} else {outer:while (pareTo(flagEnd) != 1) {cal.setTime(flag);//判断是否为节假日if (maps != null || maps.size()>0) {for (HolidayVO map : maps) {if (pareTo(map.getBeginDate()) > -1 && pareTo(map.getEndDate()) < 1) {//跳出循环进入下一个日期cal.add(Calendar.DAY_OF_MONTH, +1);flag = cal.getTime();continue outer;}}}leaveDays = leaveDays + 1;//日期往后加一天cal.add(Calendar.DAY_OF_MONTH, +1);flag = cal.getTime();}}//做小时的校验if (startHours < 8) startHours = 8;if (startHours > 17) startHours = 17;if (endHours > 17) endHours = 17;if (endHours < 8) endHours = 8;int startHour=0;if (startHours>=8&&startHours<=17){//日期+1startHour=17-startHours;//开始当天的小时计算if (startHours<=12&&startHour>0) --startHour;}int endHour=0;if (endHours>=8&&endHours<=17){endHour=endHours-8;if (endHours>=13&&endHour>0)--endHour;}//整数天hour=endHour+startHour;if (hour>=8){leaveDays=leaveDays+hour/8;hour=hour%8;}return leaveDays + "天" +hour + "小时";}