Java排除法定节假⽇、周六⽇以及⼯作⽇午休,计算两个Date
相差的时间
系统考勤请假中的⽅法:
1、在选中时间区间后,排除掉法定的节假⽇、正常双休和午休的两个⼩时,同时将调休⽇算作正常⼯作时间。
2、最后返回的天数是double类型的,是半天或是整天。
代码部分:
1、从页⾯拿到开始时间和结束时间(页⾯⽤的是vue.js),当前是String类型
/**
* 计算请假天数
*/
@Login
@RequestMapping("/calculationTime")
public R calculationTime(@RequestParam Map<String, Object> params){
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
double dayTime = 0;
dayTime = wxUserHolidayService.calculationTime(String.("stime")), String.("etime")));
double days = wxUserHolidayService.halfDayMath(dayTime);
return R.ok().put("days",days);
}
2、需要的具体⽅法,根据需要进⾏取舍
package dules.kaoqin.service;
import batisplus.service.IService;
import ity.WxUserHolidayEntity;
import java.util.*;
public interface WxUserHolidayService extends IService<WxUserHolidayEntity> {
// 获取假期⽇期
String[] holiday1();
// 获取调休⼯作⽇期
String[] holiday2();
// 获取除午休的⼯作时间
String[] workTimes();
/**
* 计算请假天数,去除周末、节假⽇
* @param stime
* @param etime
*/
double calculationTime(String stime, String etime);
/**
* 去重
* @param str
*/
List<String> removal(List<String> str);
/**
* 获取两个⽇期之间的所有⽇期,去掉周末
* @param startDate
* @param endDate
* @return
*/
List<String> getDates(String startDate, String endDate);
/**
* 字符串转时间
* @param dateStr
* @param index
* @return
*/
Date StringToDate(String dateStr, int index);
/**
* 时间转字符串
* @param date
* @param index
* @return
*/
String dateToString(Date date, int index);
/**
* 获取法定节假⽇或者调休
* @param num
* @return
string转date的方法
*/
List<String> holiday(int num);
/**
* 获取不同部门⼯作时间
* @return
*/
String[] workTime();
// 计算整天或半天
double halfDayMath(double time);
}
3、实现⽅法
package dules.kaoqin.service.impl;
import batisplus.service.impl.ServiceImpl;
import dules.kaoqin.dao.WxUserHolidayDao;
import ity.WxUserHolidayEntity;
import dules.kaoqin.service.WxQingjiaMaintainService;
import dules.kaoqin.service.WxUserHolidayService;
slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
DateFormat;
SimpleDateFormat;
import java.util.*;
@Service("wxUserHolidayService")
@Slf4j
public class WxUserHolidayServiceImpl extends ServiceImpl<WxUserHolidayDao, WxUserHolidayEntity> implements WxUserHolidayService { /**
* 定义常见的时间格式
*/
private static String[] dateFormat = {
"yyyy-MM-dd HH:mm:ss", // 0
"yyyy/MM/dd HH:mm:ss", // 1
"yyyy年MM⽉dd⽇HH时mm分ss秒", // 2
"yyyy-MM-dd", // 3
"yyyy/MM/dd", // 4
"yy-MM-dd", // 5
"yy/MM/dd", // 6
"yyyy年MM⽉dd⽇", // 7
"HH:mm:ss", // 8
"yyyyMMddHHmmss", // 9
"yyyyMMdd", // 10
"yyyy.MM.dd", // 11
"yy.MM.dd", // 12
"MM⽉dd⽇HH时mm分", // 13
"yyyy年MM⽉dd⽇ HH:mm:ss", // 14
"yyyy-MM-dd HH:mm", // 15
"yyMMdd" // 16
};
@Override
public String[] holiday1() {
return baseMapper.selectLawHolidayDate();
}
@Override
public String[] holiday2() {
return baseMapper.selectLawWorkDate();
}
@Override
public String[] workTimes() {
String S = baseMapper.selectTimeTable(Const.timeTableKey);
Map<String,Object> parse = (Map<String,Object>)JSON.parse(S);
String[] times = new String[4];
times[0] = String.("SstartTime"));
times[1] = String.("SendTime"));
times[2] = String.("XstartTime"));
times[3] = String.("XendTime"));
return times;
}
@Override
public double calculationTime(String startTime, String endTime) {
// 获取startTime和endTime之间的所有⽇期,去掉周六周⽇
List<String> list = Dates(startTime, endTime);
// 获取法定节假⽇
List<String> fdList = this.holiday(1);
// 获取调休
List<String> txList = this.holiday(2);
// 上班时间
String[] workTime = this.workTime();
String[] split = workTime[2].split(":");
String[] split1 = workTime[1].split(":");
//午休时间
int wxTime = (Integer.valueOf(split[0]) - Integer.valueOf(split1[0])) * 60;
// 删除时间区间中的所有法定节假⽇
DateFormat df = new SimpleDateFormat(dateFormat[3]);
String st =startTime.substring(0, 10);
String en =endTime.substring(0, 10);
try {
Date sts =df.parse(startTime.substring(0, 10));
Date ens =df.parse(endTime.substring(0, 10));
for (String s : txList) {
Date ss =df.parse(s);
if ((ss.before(ens) && ss.after(sts)) || ss.equals(sts) || ss.equals(ens)) {
// 添加时间区间中的所有调休⽇期
list.add(s);
}
}
}catch (Exception e){
e.printStackTrace();
}
/
/ 去重
list = val(list);
// 开始当天上午上班时间、上午下班时间、下午上班时间、下午下班时间
String amWorkYes = startTime.substring(0, 11) + workTime[0];
String amWorkNo = startTime.substring(0, 11) + workTime[1];
String pmWorkYes = startTime.substring(0, 11) + workTime[2];
String pmWorkNo = startTime.substring(0, 11) + workTime[3];
// 结束当天上午上班时间、上午下班时间、下午上班时间、下午下班时间
String amWorkYesEnd = endTime.substring(0, 11) + workTime[0];
String amWorkNoEnd = endTime.substring(0, 11) + workTime[1];
String pmWorkYesEnd = endTime.substring(0, 11) + workTime[2];
String pmWorkNoEnd = endTime.substring(0, 11) + workTime[3];
double time = 0;
if (list.size() == 0) {
// 申请⽇期是法定节假⽇
return time;
} else if (list.size() == 1) {
// 请假⼀天
if (startTimepareTo(pmWorkNo) > 0) {
return time;
}
if (endTimepareTo(amWorkYes) < 0) {
return time;
}
if (startTimepareTo(amWorkNo) >= 0 && endTimepareTo(pmWorkYes) <= 0) {
return time;
}
// 开始时间⼩于上午上班时间,开始时间等于上午上班时间
if (startTimepareTo(amWorkYes) < 0) {
startTime = amWorkYes;
}
// 结束时间⼤于下午下班时间,结束时间等于下午下班时间
if (endTimepareTo(pmWorkNo) > 0) {
endTime = pmWorkNo;
}
// 开始时间⼤于上午下班时间,⼩于下午上班时间,开始时间等于下午上班时间
if (startTimepareTo(amWorkNo) >= 0 && startTimepareTo(pmWorkYes) <= 0) {                startTime = pmWorkYes;
}
// 结束时间⼤于上午下班时间,⼩于下午上班时间,结束时间等于上午下班时间
if (endTimepareTo(amWorkNo) >= 0 && endTimepareTo(pmWorkYes) <= 0) {
endTime = amWorkNo;
}
Date start = this.StringToDate(startTime, 15); // 0或者15
Date end = this.StringToDate(endTime, 15);
// 三种情况,1:请假时间全在上午,2:请假时间全在下午,3:包含午休时间
if (startTimepareTo(amWorkYes) >= 0 && endTimepareTo(amWorkNo) <= 0) {
double minute = (Time() - Time()) / (1000 * 60);
time = minute / (8 * 60);
} else if (startTimepareTo(pmWorkYes) >= 0 && endTimepareTo(pmWorkNo) <= 0) { double minute = (Time() - Time()) / (1000 * 60);
time = minute / (8 * 60);
} else if (startTimepareTo(amWorkNo) < 0 && endTimepareTo(pmWorkYes) > 0) { double minute = (Time() - Time()) / (1000 * 60) - wxTime;
time = minute / (8 * 60);
}
return time;
} else {
// 处理请假多天的情况
// 申请开始时间处理
if (ains(st)) {
if (startTimepareTo(amWorkYes) < 0) {
startTime = amWorkYes;
}
if (startTimepareTo(pmWorkNo) > 0) {
startTime = pmWorkNo;
}
if (startTimepareTo(amWorkNo) >= 0 && startTimepareTo(pmWorkYes) <= 0) {
startTime = pmWorkYes;
}
Date start = this.StringToDate(startTime, 15); // 0或者15
Date end = this.StringToDate(pmWorkNo, 15);
if (startTimepareTo(amWorkNo) < 0) {
// 减去中午⼀⼩时
double t = (Time() - Time()) / (1000 * 60) - wxTime;
time = time + t / (8 * 60);
} else {
double t = (Time() - Time()) / (1000 * 60);
time = time + t / (8 * 60);
}
}
// 申请结束时间处理
if (ains(en)) {
if (endTimepareTo(amWorkYesEnd) < 0) {
endTime = amWorkYesEnd;
}
if (endTimepareTo(pmWorkNoEnd) > 0) {
endTime = pmWorkNoEnd;
}
if (endTimepareTo(amWorkNoEnd) >= 0 && endTimepareTo(pmWorkYesEnd) <= 0) {                    endTime = amWorkNoEnd;
}
Date end = this.StringToDate(endTime, 15);// 0或者15
Date start = this.StringToDate(amWorkYesEnd, 15);
if (endTimepareTo(pmWorkYesEnd) > 0) {
double t = (Time() - Time()) / (1000 * 60) - wxTime;
time = time + t / (8 * 60);
} else {
double t = (Time() - Time()) / (1000 * 60);
time = time + t / (8 * 60);
}
}
// 天数计算集合中剩下的个数就可以
time = time + list.size();
return time;
}
}
/**
* 去重
* @param str
*/
@Override
public List<String> removal(List<String> str) {
Set<String> s = new HashSet<String>(str);
str.clear();
str.addAll(s);
return str;
}
/**
* 获取两个⽇期之间的所有⽇期,去掉周末
* @param startDate
* @param endDate
*/
@Override
public List<String> getDates(String startDate, String endDate) {        List<String> result = new ArrayList<String>();
Calendar startDay = Instance();
Calendar endDay = Instance();
startDay.setTime(StringToDate(startDate, 3));
endDay.setTime(StringToDate(endDate, 3));
while (startDay.before(endDay)) {
int week = (Calendar.DAY_OF_WEEK);
if (7 != week && 1 != week) {
result.add(Time(), 3));
}
startDay.add(Calendar.DAY_OF_YEAR, 1);
}
// 验证结束⽇期是否是周六周⽇
int week = (Calendar.DAY_OF_WEEK);
if (7 != week && 1 != week) {
result.add(Time(), 3));
}
return result;
}
/**
* 字符串转时间
* @param dateStr
* @param index
*/
@Override
public Date StringToDate(String dateStr, int index) {
DateFormat df = null;
try {
df = new SimpleDateFormat(dateFormat[index]);
return df.parse(dateStr);
} catch (Exception aioe) {
return null;
}
}
/**
* 时间转字符串
* @param date
* @param index
*/
@Override
public String dateToString(Date date, int index) {
if (date == null) {
return null;
}
return new SimpleDateFormat(dateFormat[index]).format(date);    }
/**
* 获取法定节假⽇或者调休
* @param num
*/
@Override
public List<String> holiday(int num) {
if (num == 2) {
return Arrays.asList(this.holiday2());
} else {
return Arrays.asList(this.holiday1());
}
}
/**
* 获取不同部门⼯作时间
*/
@Override
public String[] workTime() {
return this.workTimes();
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。