public Long getWorkTime(Date startDate, Date endDate) {
String start = DateFormatUtils.format(startDate.getTime(), "yyyy-MM-dd");
String end = DateFormatUtils.format(endDate.getTime(), "yyyy-MM-dd");
List<String> specialWorkDays = this.baseMapper.selectSpecialWorkDays(start, end);
List<String> specialRestDays = this.baseMapper.selectSpecialRestDays(start, end);
return getworkTime(start, end, specialWorkDays, specialRestDays, startDate, endDate);
}
private Long getworkTime(String strStartDate, String strEndDate, List<String> specialWorkDays, List<String> specialRestDays, Date startDate, Date endDate) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Calendar cl1 = Calendar.getInstance();
Calendar cl2 = Calendar.getInstance();
try {
cl1.setTime(df.parse(strStartDate));
cl2.setTime(df.parse(strEndDate));
} catch (ParseException e) {
System.out.println("日期格式非法");
e.printStackTrace();
}
int count = 0;
while (cl1.compareTo(cl2) <= 0) {
if (cl1.get(Calendar.DAY_OF_WEEK) == 7 || cl1.get(Calendar.DAY_OF_WEEK) == 1) {
count++;
if (specialWorkDays.contains(DateFormatUtils.format(cl1.getTime(), "yyyy-MM-dd"))) {
count--;
}
}
if (specialRestDays.contains(DateFormatUtils.format(cl1.getTime(), "yyyy-MM-dd"))) {
count++;
}
cl1.add(Calendar.DAY_OF_MONTH, 1);
}
Long time= endDate.getTime() - startDate.getTime();
time = time - (count * 1000 * 60 * 60 * 24L);
return time;
}