import com.google.common.collect.Lists;
import com.wechat.third.util.FastJsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.client.RestTemplate;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @date 2019/2/19
* 调用API接口获取一年的节假日和双休日
*/
public class HolidayUtil {
@Autowired
private RestTemplate restTemplate;
public void addHolidayForOneYear() throws Exception {
List<String> dateStr = getInitMonthMapWithZero();
String apiURL = "http://www.easybots.cn/api/holiday.php?m=" + dateStr.stream().collect(Collectors.joining(","));
String result = restTemplate.getForObject(apiURL, String.class);
if (StringUtils.isNotBlank(result)) {
Map<String, Object> map = FastJsonUtils.json2map(result);
Map<String, Object> orderByResult = new LinkedHashMap<>();
map.entrySet().stream().sorted(Map.Entry.comparingByKey()).forEachOrdered( x -> orderByResult.put(x.getKey(), x.getValue()));
for (Map.Entry<String, Object> entry : orderByResult.entrySet()) {
Map<String, Object> mapValue = FastJsonUtils.json2map(orderByResult.get(entry.getKey()).toString());
Map<String, Object> orderByMapValueKeyResult = new LinkedHashMap<>();
mapValue.entrySet().stream().sorted(Map.Entry.comparingByKey()).forEachOrdered(x -> orderByMapValueKeyResult.put(x.getKey(), x.getValue()));
for (Map.Entry<String, Object> entryValue : orderByMapValueKeyResult.entrySet()) {
String holiday=LocalDate.parse(entry.getKey() + "" + entryValue.getKey(), DateTimeFormatter.ofPattern("yyyyMMdd")).toString();
System.out.println(holiday);
}
}
}
}
private List<String> getInitMonthMapWithZero() {
List<String> list = Lists.newArrayList();
LocalDate localDate = LocalDate.now();
int month = 12;
for (int j = 1; j <= month; j++) {
String date = "";
date = localDate.getYear() + (StringUtils.leftPad(String.valueOf(j), 2, "0"));
list.add(date);
}
return list;
}
public static void main(String[] args) throws Exception {
HolidayUtil util=new HolidayUtil();
util.addHolidayForOneYear();
}
}
Java通过外部API获取今年节假日和双休日
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...