免费的api接口
http://timor.tech/api/holiday
常用调用方式
http://timor.tech/api/holiday/year
http://timor.tech/api/holiday/year/2020/
http://timor.tech/api/holiday/year/2020-02
上代码
package cn.kaige.microservice.holiday;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
public class TestHoliday {
public static void main(String[] args) {
System.out.println(getAllHoliday(2024));
}
/**
* 获取周末和节假日
*
* @param year
* @return
*/
public static List<String> getAllHoliday(int year) {
// 获取所有的周末
Set<String> allWeekend = getAllWeekend(year);
// http://timor.tech/api/holiday api文档地址
Map apiHoliday = getApiHoliday(year);
Integer code = (Integer) apiHoliday.get("code");
if (code != 0) {
return null;
}
Map<String, Map<String, Object>> holiday = (Map<String, Map<String, Object>>) apiHoliday.get("holiday");
Set<String> strings = holiday.keySet();
for (String str : strings) {
Map<String, Object> stringObjectMap = holiday.get(str);
Integer wage = (Integer) stringObjectMap.get("wage");
String date = (String) stringObjectMap.get("date");
//筛选掉补班
if (wage.equals(1)) {
allWeekend.remove(date);
} else {
allWeekend.add(date);
}
}
List<String> result = new ArrayList<>(allWeekend);
result = result.stream().sorted().collect(Collectors.toList());
return result;
}
/**
* 获取节假日不含周末
*
* @param year
* @return
*/
private static Map getApiHoliday(int year) {
String url = "http://timor.tech/api/holiday/year/" + year;
String rsa = HttpUtil.get(url);
return JSONObject.parseObject(rsa, Map.class);
}
/**
* 获取周末 月从0开始
*
* @param year
* @return
*/
public static Set<String> getAllWeekend(int year) {
Set<String> dateList = new HashSet<>();
SimpleDateFormat simdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = new GregorianCalendar(year, 0, 1);
Calendar endCalendar = new GregorianCalendar(year, 11, 31);
while (true) {
int weekday = calendar.get(Calendar.DAY_OF_WEEK);
if (weekday == 1 || weekday == 7) {
dateList.add(simdf.format(calendar.getTime()));
}
calendar.add(Calendar.DATE, 1);
if (calendar.getTimeInMillis() >= endCalendar.getTimeInMillis()) {
break;
}
}
return dateList;
}
}
结果
[2024-01-01, 2024-01-06, 2024-01-07, 2024-01-13, 2024-01-14, 2024-01-20, 2024-01-21, 2024-01-27, 2024-01-28, 2024-02-03, 2024-02-10, 2024-02-11, 2024-02-12, 2024-02-13, 2024-02-14, 2024-02-15, 2024-02-16, 2024-02-17, 2024-02-24, 2024-02-25, 2024-03-02, 2024-03-03, 2024-03-09, 2024-03-10, 2024-03-16, 2024-03-17, 2024-03-23, 2024-03-24, 2024-03-30, 2024-03-31, 2024-04-04, 2024-04-05, 2024-04-06, 2024-04-13, 2024-04-14, 2024-04-20, 2024-04-21, 2024-04-27, 2024-05-01, 2024-05-02, 2024-05-03, 2024-05-04, 2024-05-05, 2024-05-12, 2024-05-18, 2024-05-19, 2024-05-25, 2024-05-26, 2024-06-01, 2024-06-02, 2024-06-08, 2024-06-09, 2024-06-10, 2024-06-15, 2024-06-16, 2024-06-22, 2024-06-23, 2024-06-29, 2024-06-30, 2024-07-06, 2024-07-07, 2024-07-13, 2024-07-14, 2024-07-20, 2024-07-21, 2024-07-27, 2024-07-28, 2024-08-03, 2024-08-04, 2024-08-10, 2024-08-11, 2024-08-17, 2024-08-18, 2024-08-24, 2024-08-25, 2024-08-31, 2024-09-01, 2024-09-07, 2024-09-08, 2024-09-15, 2024-09-16, 2024-09-17, 2024-09-21, 2024-09-22, 2024-09-28, 2024-10-01, 2024-10-02, 2024-10-03, 2024-10-04, 2024-10-05, 2024-10-06, 2024-10-07, 2024-10-13, 2024-10-19, 2024-10-20, 2024-10-26, 2024-10-27, 2024-11-02, 2024-11-03, 2024-11-09, 2024-11-10, 2024-11-16, 2024-11-17, 2024-11-23, 2024-11-24, 2024-11-30, 2024-12-01, 2024-12-07, 2024-12-08, 2024-12-14, 2024-12-15, 2024-12-21, 2024-12-22, 2024-12-28, 2024-12-29]
参考
https://blog.csdn.net/weixin_44299027/article/details/116028963