import org.springframework.web.client.RestTemplate;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* 节假日爬虫(接口调用失败:默认取所有周末的日期)
*/
public class HolidayCrawler {
private static final String YEAR_HOLIDAY_URL = "https://api.apihubs.cn/holiday/get?field=date,weekend&year=";
private static final String YEAR_HOLIDAY_PARAMS = "&workday=2&order_by=1&cn=1&page=1&size=500";
/**
* http请求工具类
*/
private static final RestTemplate REST_TEMPLATE = new RestTemplate();
/**
* 获取指定年份节假日
* @param year
* @return
*/
public static List<String> initHolidays(String year){
List<String> Holidays = new ArrayList<>();
try {
long ts = System.currentTimeMillis();
Map<String,Object> result = REST_TEMPLATE.getForObject(String.format(YEAR_HOLIDAY_URL+ year + YEAR_HOLIDAY_PARAMS, ts, ts), Map.class);
if(null != result){
String code = String.valueOf(result.get("code"));
String msg = String.valueOf(result.get("msg"));
if(code.equals("0") && msg.equals("ok")){
Map data = (Map) result.get("data");
if(null != data){
List<Map<String,Object>> list = (List<Map<String, Object>>) data.get("list");
for (Map<String,Object> d : list){
String date = d.get("date").toString();
String formatDate = date.substring(0, 4)+"-"+date.substring(4, 6)+"-"+date.substring(6, 8);
Holidays.add(formatDate);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
if (Holidays.isEmpty()) {
Holidays = getWeekdays(Integer.parseInt(year));
}
return Holidays;
}
/**
* 获取指定年份的所有周末日期
* @param year 年份
*/
public static List<String> getWeekdays(int year){
List<String> dateList= new ArrayList<>();
SimpleDateFormat simdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = new GregorianCalendar(year, 0, 1);
int i = 1;
while (calendar.get(Calendar.YEAR) < year + 1) {
calendar.set(Calendar.WEEK_OF_YEAR, i++);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
if (calendar.get(Calendar.YEAR) == year) {
dateList.add(simdf.format(calendar.getTime()));
}
calendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
if (calendar.get(Calendar.YEAR) == year) {
dateList.add(simdf.format(calendar.getTime()));
}
}
return dateList;
}
/**
* 日期范围 - 切片
*
* @param startDate 起始日期
* @param endDate 结束日期
* @param type 切片类型 1年 2月 3日 4小时
* @return 切片日期
*/
public static List<String> sliceUpDateRange(String startDate, String endDate, int type) {
List<String> rs = new ArrayList<>();
try {
int dt = Calendar.DATE;
String pattern = "yyyy-MM-dd";
if (type == 1) {
pattern = "yyyy";
dt = Calendar.YEAR;
} else if (type == 2) {
pattern = "yyyy-MM";
dt = Calendar.MONTH;
} else if (type == 3) {
pattern = "yyyy-MM-dd";
dt = Calendar.DATE;
} else if (type == 4) {
pattern = "yyyy-MM-dd HH";
dt = Calendar.HOUR_OF_DAY;
}
SimpleDateFormat sd = new SimpleDateFormat(pattern);
Calendar sc = Calendar.getInstance();
Calendar ec = Calendar.getInstance();
sc.setTime(sd.parse(startDate));
ec.setTime(sd.parse(endDate));
while (sc.compareTo(ec) < 1) {
rs.add(sd.format(sc.getTime()));
sc.add(dt, 1);
}
} catch (ParseException e) {
e.printStackTrace();
}
return rs;
}
}
java节假日爬虫(接口调用失败:默认取所有周末的日期)
最新推荐文章于 2024-08-16 09:40:33 发布