您好,登錄后才能下訂單哦!
本篇內容主要講解“Java小程序怎么實現獲取國家節假日”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“Java小程序怎么實現獲取國家節假日”吧!
此節假日為嚴格按照國家要求的雙休和法定節假日并且包含節假日的補班信息,大家可根據自己的需求自定義處理哦。
以下為Maven配置,是程序用到的依賴。版本的話,可以用最新的。
<!-- okhttp -->
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>okhttp</artifactId>
<version>${okhttp.version}</version>
</dependency>
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
package com.uiotsoft.daily.task;
import com.alibaba.fastjson.JSONObject;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* <p>TestDate 此類用于:</p>
* <p>@author:hujm</p>
* <p>@date:2021年04月22日 17:43</p>
* <p>@remark:</p>
*/
public class TestDate {
public static void main(String[] args) {
System.out.println(getJjr(2021, 4));
System.out.println(getMonthWekDay(2021, 4));
System.out.println(JJR(2021, 4));
}
/**
* 獲取周末和節假日
*
* @param year
* @param month
* @return
*/
public static Set<String> JJR(int year, int month) {
//獲取所有的周末
Set<String> monthWekDay = getMonthWekDay(year, month);
//http://timor.tech/api/holiday api文檔地址
Map jjr = getJjr(year, month + 1);
Integer code = (Integer) jjr.get("code");
if (code != 0) {
return monthWekDay;
}
Map<String, Map<String, Object>> holiday = (Map<String, Map<String, Object>>) jjr.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)) {
monthWekDay.remove(date);
} else {
monthWekDay.add(date);
}
}
return monthWekDay;
}
/**
* 獲取節假日不含周末
*
* @param year
* @param month
* @return
*/
private static Map getJjr(int year, int month) {
String url = "http://timor.tech/api/holiday/year/";
OkHttpClient client = new OkHttpClient();
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();
} catch (IOException e) {
e.printStackTrace();
}
return JSONObject.parseObject(rsa, Map.class);
}
/**
* 獲取周末 月從0開始
*
* @param year
* @param mouth
* @return
*/
public static Set<String> getMonthWekDay(int year, int mouth) {
Set<String> dateList = new HashSet<>();
SimpleDateFormat simdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = new GregorianCalendar(year, mouth, 1);
Calendar endCalendar = new GregorianCalendar(year, mouth, 1);
endCalendar.add(Calendar.MONTH, 1);
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;
}
}
以上方法可以拿來即用,當然也可以根據自己的需求自定義。
以下是我自己業務需求,將調用API接口獲取的節假日信息保存到本地數據庫中,如果不感興趣可以跳過以下內容哦~~~~
package com.uiotsoft.daily.task;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.uiotsoft.daily.module.entity.DailyHolidayConfig;
import com.uiotsoft.daily.module.entity.HolidayRawInfo;
import com.uiotsoft.daily.module.service.DailyHolidayConfigService;
import com.uiotsoft.daily.module.service.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
/**
* <p>NoSubmitTask 此類用于:</p>
* <p>@author:hujm</p>
* <p>@date:2021年04月16日 17:10</p>
* <p>@remark:</p>
*/
@Slf4j
@Component
public class NoSubmitTask {
@Resource
private DailyHolidayConfigService holidayConfigService;
@Value("${syncAddress}")
private String syncAddress;
@Scheduled(cron = "${syncHolidayDeadline}")
public void syncHoliday() {
log.info("每年12月28凌晨1點定時同步下一年的節假日信息,同步節假日開始時間 = {}", DateUtil.formatDateTime(new Date()));
String url = syncAddress;
OkHttpClient client = new OkHttpClient();
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();
} catch (IOException e) {
e.printStackTrace();
}
Map map = JSONObject.parseObject(rsa, Map.class);
if (map != null) {
Integer code = (Integer) map.get("code");
if (code == 0) {
JSONObject holidayJson = (JSONObject) map.get("holiday");
String jsonString = holidayJson.toJSONString();
log.info("獲取節假日數據內容為 jsonString = 【{}】", jsonString);
Set<Map.Entry<String, Object>> entrySet = holidayJson.entrySet();
List<HolidayRawInfo> rawInfoList = new ArrayList<>();
for (Map.Entry<String, Object> entry : entrySet) {
String key = entry.getKey();
Object value = entry.getValue();
cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(value);
HolidayRawInfo holidayRawInfo = JSONUtil.toBean(jsonObject, HolidayRawInfo.class);
rawInfoList.add(holidayRawInfo);
}
// 定義節假日集合
List<DailyHolidayConfig> holidayConfigList = new ArrayList<>();
for (HolidayRawInfo holidayRawInfo : rawInfoList) {
DailyHolidayConfig holidayConfig = new DailyHolidayConfig();
holidayConfig.setHolidayTarget(holidayRawInfo.getTarget());
holidayConfig.setHolidayAfter(holidayRawInfo.getAfter());
holidayConfig.setHolidayDate(holidayRawInfo.getDate());
holidayConfig.setHolidayName(holidayRawInfo.getName());
holidayConfig.setHolidayRest(holidayRawInfo.getRest());
holidayConfig.setHolidayWage(holidayRawInfo.getWage());
holidayConfig.setCreateTime(new Date());
holidayConfigList.add(holidayConfig);
}
// 根據日期排序升序
List<DailyHolidayConfig> collect = holidayConfigList.stream().sorted(Comparator.comparing(DailyHolidayConfig::getHolidayDate)).collect(Collectors.toList());
// 批量插入節假日表中
holidayConfigService.insertBatch(collect);
} else {
log.error("E|NoSubmitTask|syncHoliday()|同步節假日信息時,調用節假日網站服務出錯!");
}
}
log.info("每年12月28凌晨1點定時同步下一年的節假日信息,同步節假日結束時間 = {}", DateUtil.formatDateTime(new Date()));
}
}
到此,相信大家對“Java小程序怎么實現獲取國家節假日”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。