一、工具类
public class VacationUtil {
private static final Map<String,String> V_STATUS_MAP = new HashMap<>(){{
put("0","工作日");
put("1","休息日");
put("2","节假日");
}};
private static final Map<String,String> DATE_TYPE_MAP = new HashMap<>(){{
put("4","yyyy");
put("6","yyyyMM");
put("8","yyyyMMdd");
}};
public static <T> T getVacationData(String apikey,String date){
if(!isLegalDate(date)){
throw new RuntimeException("Illegitimate date");
}
Map<String,String> map = new HashMap<>();
String res="";
try {
res = HttpUtil.get("http://tool.bitefu.net/jiari/vip.php?d="+date+"&apikey="+apikey+"&type=3&backtype=1");
}catch (Exception e){
throw new RuntimeException("getVacationData request exception");
}
if(res == null && res == ""){
return (T)map;
}
JSONObject jsonObject = JSONObject.parseObject(res);
if("0".equals(jsonObject.getString("status"))){
throw new RuntimeException(jsonObject.getString("msg"));
}
if(date.length()==8){
return (T)jsonObject.getString("data");
}
map = (Map<String,String>)jsonObject.get("data");
return (T)map;
}
private static boolean isLegalDate(String date){
if(date == null || (date.length() != 4 && date.length() != 6 && date.length() != 8)){
return false;
}
SimpleDateFormat sdf = new SimpleDateFormat(DATE_TYPE_MAP.get(String.valueOf(date.length())));
sdf.setLenient(false);
try {
sdf.parse(date);
} catch (ParseException e) {
return false;
}
return true;
}
public static String convert(String code){
return V_STATUS_MAP.get(code);
}
}
二、测试
public static void main(String[] args) {
Map<String,String> res1 = VacationUtil.getVacationData("123456","202408");
System.out.println(res1);
String res2 = VacationUtil.getVacationData("123456","20240827");
System.out.println(res2);
}
{"20240814":0,"20240813":0,"20240812":0,"20240811":1,"20240818":1,"20240817":1,"20240816":0,"20240815":0,"20240810":1,"20240831":1,"20240830":0,"20240819":0,"20240803":1,"20240825":1,"20240802":0,"20240824":1,"20240801":0,"20240823":0,"20240822":0,"20240807":0,"20240829":0,"20240806":0,"20240828":0,"20240805":0,"20240827":0,"20240804":1,"20240826":0,"20240821":0,"20240820":0,"20240809":0,"20240808":0}
0