【java】实用的计算工作日方法(包含计算两个日期之间的工作日、判断是否是工作日、计算几个工作日后的日期)
该方法主要需要创建存储工作日和节假日的信息表(JjrbdbDTO),然后只需要有一个查询list的方法(JjrbdbMapper),然后根据想要的操作,去调用下面封装好的方法即可
准备工作
首先需要创建一个存储时间信息的表,具体字段如下:
具体实现方法
//具体方法
import com.ahylz.system.dto.JjrbdbDTO;
import com.ahylz.system.mapper.JjrbdbMapper;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
/***
* @Auther: lx
*/
@Service
public class HolidayUtil {
@Autowired
private JjrbdbMapper jjrbdbMapper;
/***
* 计算两个时间还剩多少个工作日
* @param startTime 开始时间
* @param endTime 结束时间
* @return 两者之间还差多少个工作日
* @throws ParseException
*/
public int calWorkingDays(Date startTime, Date endTime) throws ParseException {
int Days = 0;
boolean a = false;
//从startTime开始循环,若该日期不是节假日或者不是周六日则天数+1
Date flag = startTime;//设置循环开始日期
Calendar cal = Calendar.getInstance();
Date time = cal.getTime();
while (a == false) {
cal.setTime(flag);
a = isWorkingDay(cal.getTime());
if (a == false) {
cal.add(Calendar.DAY_OF_MONTH, +1);
flag = cal.getTime();
Days = 1;
}
}
//循环遍历每个日期
while (flag.compareTo(endTime) != 1) {
cal.setTime(flag);
Boolean workingDay = isWorkingDay(cal.getTime());
if (workingDay) {
//不是节假日或者周末,天数+1
Days = Days + 1;
}
//日期往后加一天
cal.add(Calendar.DAY_OF_MONTH, +1);
flag = cal.getTime();
}
return Days;
}
/***
* 计算一个添加几个工作日后最终的日期(排除维护的节假日和包含维护的工作日)
* @param startTime 开始时间
* @param workDay 添加的工作日数
* @return 添加工作日后的时间
* @throws ParseException
*/
public Date getWorkingDay(Date startTime, int workDay) throws ParseException {
int Days = 0;
boolean a = false;
boolean b = false;
//从startTime开始循环,若该日期不是节假日或者不是周六日则天数+1
Date flag = startTime;//设置循环开始日期
Calendar cal = Calendar.getInstance();
while (a == false) {
cal.setTime(flag);
a = isWorkingDay(cal.getTime());
if (a == false) {
//日期往后加一天
cal.add(Calendar.DAY_OF_MONTH, +1);
flag = cal.getTime();
Days = 1;
}
}
//循环遍历每个日期
while (Days != workDay) {
cal.setTime(flag);
Boolean workingDay = isWorkingDay(cal.getTime());
if (workingDay) {
//不是节假日或者周末,天数+1
Days = Days + 1;
}
//日期往后加一天
cal.add(Calendar.DAY_OF_MONTH, +1);
flag = cal.getTime();
}
while (b == false) {
cal.setTime(flag);
b = isWorkingDay(cal.getTime());
if (b == false) {
//日期往后加一天
cal.add(Calendar.DAY_OF_MONTH, +1);
flag = cal.getTime();
}
}
return flag;
}
public static LocalDate DateToLocalDate(Date date) {
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
}
/***
* 判断该天是不是工作日(日期格式)
* @param date 需要判断的时间
* @return true 是工作日 false 非工作日
* @throws ParseException
*/
public Boolean isWorkingDay(Date date) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String time = sdf.format(date);
// 获取数据库保存的假期和工作日的信息
List<JjrbdbDTO> list = jjrbdbMapper.queryListByTime(time);
for (int a = 0; a < list.size(); a++) {
JjrbdbDTO dto = list.get(a);
String gzbz = dto.getGzbz();
String stime = dto.getStime();
String etime = dto.getEtime();
int x = Integer.parseInt(time);
int y = Integer.parseInt(stime);
int z = Integer.parseInt(etime);
if (gzbz.equals("1")) {
//休息日
if (x >= y && x <= z) {
return false;
} else {
continue;
}
} else {
//工作日
if (x >= y && x <= z) {
return true;
} else {
continue;
}
}
}
LocalDate dateTime = HolidayUtil.DateToLocalDate(date);
//如果是1-5表示周一到周五 是工作日
DayOfWeek week = dateTime.getDayOfWeek();
if (week == DayOfWeek.SATURDAY || week == DayOfWeek.SUNDAY) {
return false;
}
return true;
}
/***
* 判断该天是不是工作日(string格式)
* @param time 需要判断的时间
* @return true 是工作日 false 非工作日
* @throws ParseException
*/
public Boolean isWorkDay(String time) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date date = sdf.parse(time);
List<JjrbdbDTO> list = jjrbdbMapper.queryListByTime(time);
for (int a = 0; a < list.size(); a++) {
JjrbdbDTO dto = list.get(a);
String gzbz = dto.getGzbz();
String stime = dto.getStime();
String etime = dto.getEtime();
int x = Integer.parseInt(time);
int y = Integer.parseInt(stime);
int z = Integer.parseInt(etime);
if (gzbz.equals("1")) {
//休息日
if (x >= y && x <= z) {
return false;
} else {
continue;
}
} else {
//工作日
if (x >= y && x <= z) {
return true;
} else {
continue;
}
}
}
LocalDate dateTime = HolidayUtil.DateToLocalDate(date);
//如果是1-5表示周一到周五 是工作日
DayOfWeek week = dateTime.getDayOfWeek();
if (week == DayOfWeek.SATURDAY || week == DayOfWeek.SUNDAY) {
return false;
}
return true;
}
@Test
public void test() throws ParseException {
String dateStr2 = "2023-01-08";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = sdf.parse(dateStr2);
Date date2 = getWorkingDay(date1, 6);
System.out.println(sdf.format(date2));
System.out.println("工作日:" + calWorkingDays(date1, date2));
}
}