工具类
package org.jsola.salary.common;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ResourceUtils;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* 统计工作日
*
* @author wcl
*/
public class WorkdayKit {
/**
* 将时间格式化为YYYY-MM-dd类型
*
* @param date 传入时间
* @return 格式化后的时间
*/
private static String formatDate(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd");
return sdf.format(date);
}
private final static Logger logger = LoggerFactory.getLogger(WorkdayKit.class);
/**
* 传入日期是否为工作日
*
* @param date 日期
* @return true:工作日 false:休息日
*/
public static boolean isWorkDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int year = calendar.get(Calendar.YEAR);
File jsonFile = null;
try {
jsonFile = ResourceUtils.getFile("classpath:workday.json");
} catch (FileNotFoundException e) {
logger.error("WorkdayKit读取文件出错");
e.printStackTrace();
}
String json = null;
try {
json = FileUtils.readFileToString(jsonFile, "UTF-8");
} catch (IOException e) {
logger.error("WorkdayKit读取文件数据出错");
e.printStackTrace();
}
JSONArray jsonArray = JSON.parseArray(json);
// 节假日
List<String> holidayList = new ArrayList<>();
// 加班日
List<String> overtimeList = new ArrayList<>();
for (Object obj : jsonArray) {
JSONObject jsonObject = (JSONObject) obj;
if (jsonObject.get("year").equals(year + "")) {
Map map = JSONObject.toJavaObject(jsonObject, Map.class);
Object holiday = map.get("holiday");
holidayList = (List<String>) holiday;
Object overtime = map.get("overtime");
overtimeList = (List<String>) overtime;
break;
}
}
// 传入日期是否属于节假日
String format = formatDate(date);
if (holidayList.contains(format)) {
return false;
}
// 传入日期是否属于加班日
if (overtimeList.contains(format)) {
return true;
}
// 传入日期是否属于周末
int day = calendar.get(Calendar.DAY_OF_WEEK);
if (day != Calendar.SATURDAY && day != Calendar.SUNDAY) {
return true;
}
return false;
}
/**
* 获取某时间段内的工作日,若结束时间是工作日,计入工作日
*
* @param dateStart 开始时间
* @param dateEnd 结束时间
* @return 工作日列表
*/
public static List<String> workdayList(Date dateStart, Date dateEnd) {
List<String> list = new ArrayList<>();
SimpleDateFormat outformat = new SimpleDateFormat("yyyy-MM-dd");
if (dateStart.compareTo(dateEnd) == 0) {
if (isWorkDay(dateStart)) {
list.add(outformat.format(dateStart));
return list;
}
return null;
}
Calendar sCalendar = Calendar.getInstance();
sCalendar.setTime(dateStart);
int year = sCalendar.get(Calendar.YEAR);
int month = sCalendar.get(Calendar.MONTH);
int day = sCalendar.get(Calendar.DATE);
sCalendar.set(year, month, day, 0, 0, 0);
Calendar eCalendar = Calendar.getInstance();
eCalendar.setTime(dateEnd);
year = eCalendar.get(Calendar.YEAR);
month = eCalendar.get(Calendar.MONTH);
day = eCalendar.get(Calendar.DATE);
eCalendar.set(year, month, day, 23, 59, 59);
while (sCalendar.before(eCalendar)) {
if (isWorkDay(sCalendar.getTime())) {
list.add(outformat.format(sCalendar.getTime()));
}
sCalendar.add(Calendar.DAY_OF_YEAR, 1);
}
return list;
}
/**
* 获取某时间段内的工作日,若结束时间是工作日,不计入工作日
*
* @param dateStart 开始时间
* @param dateEnd 结束时间
* @return 工作日列表
*/
public static List<String> workdayListNotIncludeEndDate(Date dateStart, Date dateEnd) {
List<String> list = new ArrayList<>();
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateEnd);
calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) - 1);
dateEnd = calendar.getTime();
if (dateStart.compareTo(dateEnd) == 0) {
if (isWorkDay(dateStart)) {
list.add(formatDate(dateStart));
return list;
}
return null;
}
Calendar sCalendar = Calendar.getInstance();
sCalendar.setTime(dateStart);
int year = sCalendar.get(Calendar.YEAR);
int month = sCalendar.get(Calendar.MONTH);
int day = sCalendar.get(Calendar.DATE);
sCalendar.set(year, month, day, 0, 0, 0);
Calendar eCalendar = Calendar.getInstance();
eCalendar.setTime(dateEnd);
year = eCalendar.get(Calendar.YEAR);
month = eCalendar.get(Calendar.MONTH);
day = eCalendar.get(Calendar.DATE);
eCalendar.set(year, month, day, 23, 59, 59);
while (sCalendar.before(eCalendar)) {
if (isWorkDay(sCalendar.getTime())) {
list.add(formatDate(sCalendar.getTime()));
}
sCalendar.add(Calendar.DAY_OF_YEAR, 1);
}
return list;
}
}
workday.json 需要手动维护每年的法定节假日与调休日 year:年份 holiday:法定节假日 overtime:调休上班日
[
{
"year": "2020",
"holiday": [
"2020-01-01",
"2020-01-24",
"2020-01-25",
"2020-01-26",
"2020-01-27",
"2020-01-28",
"2020-01-29",
"2020-01-30",
"2020-01-31",
"2020-02-01",
"2020-02-02",
"2020-04-04",
"2020-04-05",
"2020-04-06",
"2020-05-01",
"2020-05-02",
"2020-05-03",
"2020-05-04",
"2020-05-05",
"2020-06-25",
"2020-06-26",
"2020-06-27",
"2020-10-01",
"2020-10-02",
"2020-10-03",
"2020-10-04",
"2020-10-05",
"2020-10-06",
"2020-10-07",
"2020-10-08"
],
"overtime": [
"2020-01-19",
"2020-04-26",
"2020-05-09",
"2020-06-28",
"2020-09-27",
"2020-10-10"
]
},
{
"year": "2020",
"holiday": [
"2020-01-01",
"2020-01-02",
"2020-01-03",
"2020-02-11",
"2020-02-12",
"2020-02-14",
"2020-02-15",
"2020-02-16",
"2020-02-17",
"2020-04-03",
"2020-04-04",
"2020-04-05",
"2020-05-01",
"2020-05-02",
"2020-05-03",
"2020-05-04",
"2020-05-05",
"2020-06-12",
"2020-06-13",
"2020-06-14",
"2020-09-19",
"2020-09-20",
"2020-09-21",
"2020-10-01",
"2020-10-02",
"2020-10-03",
"2020-10-04",
"2020-10-05",
"2020-10-06",
"2020-10-07"
],
"overtime": [
"2020-02-07",
"2020-02-20",
"2020-04-25",
"2020-05-08",
"2020-01-03",
"2020-09-18",
"2020-09-26",
"2020-10-09"
]
}
]