Java获取国家节假日规定休息日期
private static Map<String, JSONArray> getJjr(int year) {
String url = "http://timor.tech/api/holiday/year/"+year;
OkHttpClient client = new OkHttpClient();
Map<String, String> dateMap = new HashMap<>();
List<JSONObject> list = new ArrayList();
JSONObject jsonObject ;
Response response;
String rsa = null;
Request request = new Request.Builder()
.url(url)
.get()
.addHeader("Content-Type", "application/x-www-form-urlencoded")
.build();
try {
response = client.newCall(request).execute();
rsa = response.body().string();
Map map = JSONObject.parseObject(rsa, Map.class);
Map<String, Map<String, Object>> holiday = (Map<String, Map<String, Object>>) map.get("holiday");
Set<String> strings = holiday.keySet();
for (String str : strings) {
Map<String, Object> stringObjectMap = holiday.get(str);
boolean bool = (Boolean) stringObjectMap.get("holiday");
if (bool) {
jsonObject = new JSONObject();
String substring = str.substring(0, 2);
jsonObject.put(substring, stringObjectMap);
list.add(jsonObject);
}
}
Map<String, JSONArray> jsonArrayMap = new HashMap<>();
for (int i = 1; i < 11; i++) {
JSONArray jsonArray = new JSONArray();
String keyMap = null;
for (JSONObject object : list) {
if (i < 10 && object.containsKey("0"+i)){
keyMap = "0" + i;
jsonArray.add(object.get("0"+i));
}else if (i >= 10 && object.containsKey("1"+i)){
jsonArray.add(object.get("1"+i));
keyMap = "1" + i;
}
}
jsonArrayMap.put(keyMap, jsonArray);
}
return jsonArrayMap;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private static Map<String, JSONArray> getJjr(int year, String month) {
String url = "http://timor.tech/api/holiday/year/"+year+"-"+month;
OkHttpClient client = new OkHttpClient();
Map<String, String> dateMap = new HashMap<>();
List<JSONObject> list = new ArrayList();
JSONObject jsonObject ;
Response response;
String rsa = null;
Request request = new Request.Builder()
.url(url)
.get()
.addHeader("Content-Type", "application/x-www-form-urlencoded")
.build();
try {
response = client.newCall(request).execute();
rsa = response.body().string();
Map map = JSONObject.parseObject(rsa, Map.class);
Map<String, Map<String, Object>> holiday = (Map<String, Map<String, Object>>) map.get("holiday");
Set<String> strings = holiday.keySet();
for (String str : strings) {
Map<String, Object> stringObjectMap = holiday.get(str);
boolean bool = (Boolean) stringObjectMap.get("holiday");
if (bool) {
jsonObject = new JSONObject();
String substring = str.substring(0, 2);
jsonObject.put(substring, stringObjectMap);
list.add(jsonObject);
}
}
Map<String, JSONArray> jsonArrayMap = new HashMap<>();
for (int i = 1; i < 11; i++) {
JSONArray jsonArray = new JSONArray();
String keyMap = null;
for (JSONObject object : list) {
if (i < 10 && object.containsKey("0"+i)){
keyMap = "0" + i;
jsonArray.add(object.get("0"+i));
}else if (i >= 10 && object.containsKey("1"+i)){
jsonArray.add(object.get("1"+i));
keyMap = "1" + i;
}
}
jsonArrayMap.put(keyMap, jsonArray);
}
return jsonArrayMap;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
示例 执行 Main方法
public class Test {
public static void main(String[] args) throws Exception {
Calendar cal = Calendar.getInstance();
int month = cal.get(Calendar.MONTH) + 1;
int year = cal.get(Calendar.YEAR);
System.out.println(year);
System.out.println(month);
String month1 = month > 10 ? String.valueOf(month) : "0" + month;
Map<String, JSONArray> stringJSONArrayMap = getJjr(year, month1);
JSONArray jsonArray = stringJSONArrayMap.get(month1);
System.out.println(JSONObject.toJSONString(jsonArray));
}
private static Map<String, JSONArray> getJjr(int year, String month) {
String url = "http://timor.tech/api/holiday/year/"+year+"-"+month;
OkHttpClient client = new OkHttpClient();
Map<String, String> dateMap = new HashMap<>();
List<JSONObject> list = new ArrayList();
JSONObject jsonObject ;
Response response;
String rsa = null;
Request request = new Request.Builder()
.url(url)
.get()
.addHeader("Content-Type", "application/x-www-form-urlencoded")
.build();
try {
response = client.newCall(request).execute();
rsa = response.body().string();
Map map = JSONObject.parseObject(rsa, Map.class);
Map<String, Map<String, Object>> holiday = (Map<String, Map<String, Object>>) map.get("holiday");
Set<String> strings = holiday.keySet();
for (String str : strings) {
Map<String, Object> stringObjectMap = holiday.get(str);
boolean bool = (Boolean) stringObjectMap.get("holiday");
if (bool) {
jsonObject = new JSONObject();
String substring = str.substring(0, 2);
jsonObject.put(substring, stringObjectMap);
list.add(jsonObject);
}
}
Map<String, JSONArray> jsonArrayMap = new HashMap<>();
for (int i = 1; i < 11; i++) {
JSONArray jsonArray = new JSONArray();
String keyMap = null;
for (JSONObject object : list) {
if (i < 10 && object.containsKey("0"+i)){
keyMap = "0" + i;
jsonArray.add(object.get("0"+i));
}else if (i >= 10 && object.containsKey("1"+i)){
jsonArray.add(object.get("1"+i));
keyMap = "1" + i;
}
}
jsonArrayMap.put(keyMap, jsonArray);
}
return jsonArrayMap;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
|