在此感谢节假日API提供者。
实体(根据需求确认是否需要),需要重写hashCode、equalsimport lombok.Data;
import java.time.LocalDateTime;
@Data
public class HolidayEntity {
private Integer id;
private String date;
private String dateType;
private String dateRemark;
private Integer ofYear;
private Integer status;
private LocalDateTime createdDate;
private String createdBy;
private LocalDateTime updatedDate;
private String updatedBy;
@Override
public boolean equals(Object obj) {
// System.out.println("调用equals方法,当前的hashCode为:"+hashCode());
if (obj == null) {
return false;
}
if (this == obj) {
return true;
}
if (obj instanceof HolidayEntity) {
HolidayEntity vo = (HolidayEntity) obj;
if (vo.date.equals(this.date)) {
return true;
}
}
return false;
}
@Override
public int hashCode() {
return this.getDate().hashCode() * this.getDate().hashCode();
}
}
HolidayUtil
import cn.lin.entity.HolidayEntity;
import com.alibaba.fastjson.JSONObject;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
public class HolidayUtil {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
List holidayEntities = new ArrayList<>();
for (int i = 1; i < 13; i++) {
String format = new DecimalFormat("00").format(Integer.valueOf(i));
Set holiday = getHoliday(year, format);
holidayEntities.addAll(holiday);
}
System.out.println(1);
}
public static Set getHoliday(int year, String month){
Set set = new HashSet<>();
// 获取节假日不含周末------获取指定月份
String url = "http://timor.tech/api/holiday/year/"+year+"-"+month;
OkHttpClient client = new OkHttpClient();
Response response;
//解密数据
String rsa = null;
Request request = new Request.Builder()
.url(url)
.get()
.addHeader("Content-Type", "application/x-www-form-urlencoded")
.build();
// 补班的日期
List dateWorks = new ArrayList<>();
try {
response = client.newCall(request).execute();
rsa = response.body().string();
Map map = JSONObject.parseObject(rsa, Map.class);
Map> holiday = (Map>) map.get("holiday");
Set strings = holiday.keySet();
for (String str : strings) {
Map stringObjectMap = holiday.get(str);
boolean bool = (Boolean) stringObjectMap.get("holiday");
//筛选掉补班
if (bool) {
String date = (String) stringObjectMap.get("date");
HolidayEntity holidayEntity = new HolidayEntity();
holidayEntity.setDate(date);
holidayEntity.setDateType("法定节假日");
holidayEntity.setDateRemark(String.valueOf(stringObjectMap.get("name")));
set.add(holidayEntity);
} else {
String date = (String) stringObjectMap.get("date");
dateWorks.add(date);
}
}
} catch (IOException e) {
e.printStackTrace();
}
// 获取所有周末
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, Integer.valueOf(month) - 1);
cal.set(Calendar.DATE, 1);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
while (cal.get(Calendar.YEAR) == year &&
cal.get(Calendar.MONTH) < Integer.valueOf(month)) {
int day = cal.get(Calendar.DAY_OF_WEEK);
Date date = (Date)cal.getTime().clone();
String time = simpleDateFormat.format(date);
if (day == Calendar.SUNDAY && !dateWorks.contains(time)) { // 排除周末排班
HolidayEntity holidayEntity = new HolidayEntity();
holidayEntity.setDate(time);
holidayEntity.setDateType("休息日");
holidayEntity.setDateRemark("周日");
set.add(holidayEntity);
}
if ( day == Calendar.SATURDAY && !dateWorks.contains(time)) { // 排除周末排班
HolidayEntity holidayEntity = new HolidayEntity();
holidayEntity.setDate(time);
holidayEntity.setDateType("休息日");
holidayEntity.setDateRemark("周六");
set.add(holidayEntity);
}
cal.add(Calendar.DATE, 1);
}
return set;
}
}



