获取指定月份的所有工作日(工作日为自定义的)
场景
在创建任务的时候,会根据工作日去批量生成,但是不同厂的工作日不是相当的,也就是我们需要根据不同的业务进行工作日的定制
时间工具类 (根据不同的需求,可以对返回结果进行对应的修改)
代码语言:javascript复制/**
* 获取指定月份的假日
* @param month
* @return
*/
public static Map<String,Map<String,String>> getCustomDays(String month , String workdays ){
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat dateFm = new SimpleDateFormat("EEEE");
Map<String,Map<String,String>> res = new HashMap<>();
// 1. 获取当前月所有的天
Integer year = Integer.parseInt(month.substring(0,4));
Integer months = Integer.parseInt(month.substring(4));
Calendar start = Calendar.getInstance();
start.set(year,months-1,1);
List<String> days = new ArrayList<>();
if(workdays.contains(",")){
days= Arrays.asList(workdays.split(","));
}else{
days.add(workdays);
}
Map<String,String> outHoliday = new HashMap<>();
List<String> workDay = new ArrayList<>();
while (start.get(Calendar.MONTH) < months){
Date date = start.getTime();
String currSun = dateFm.format(date);
days.forEach(e -> {
if(e.equals(currSun)) {
workDay.add(sdf.format(start.getTime().clone()));
outHoliday.put(sdf.format(start.getTime().clone()),"0");
}
});
start.add(Calendar.DATE,1);
}
res.put(month,outHoliday);
return res;
}
测试
代码语言:javascript复制public static void main(String[] args) {
getCustomDays("202007","星期一,星期二,星期三");
}