教你怎么⽤Java获取国家法定节假⽇
前⾔
此节假⽇为严格按照国家要求的双休和法定节假⽇并且包含节假⽇的补班信息,⼤家可根据⾃⼰的需求⾃定义处理哦。以下为Maven配置,是程序⽤到的依赖。版本的话,可以⽤最新的。
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>
Java程序
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;
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);
//h/api/holiday api⽂档地址
Map jjr = getJjr(year, month + 1);
Integer code = (Integer) ("code");
if (code != 0) {
return monthWekDay;
}
Map<String, Map<String, Object>> holiday = (Map<String, Map<String, Object>>) ("holiday");
Set<String> strings = holiday.keySet();
for (String str : strings) {
Map<String, Object> stringObjectMap = (str);
Integer wage = (Integer) ("wage");
String date = (String) ("date");
//筛选掉补班
if (wage.equals(1)) {
} else {
monthWekDay.add(date);
}
}
return monthWekDay;
}
/**
* 获取节假⽇不含周末
*
* @param year
* @param month
* @return
*/
private static Map getJjr(int year, int month) {
String url = "h/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 = wCall(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.DAY_OF_WEEK);
if (weekday == 1 || weekday == 7) {
dateList.add(simdf.Time()));
}
calendar.add(Calendar.DATE, 1);
if (TimeInMillis() >= TimeInMillis()) {
break;
}
}
return dateList;fastjson怎么用
}
}
以上⽅法可以拿来即⽤,当然也可以根据⾃⼰的需求⾃定义。
以下是我⾃⼰业务需求,将调⽤API接⼝获取的节假⽇信息保存到本地数据库中,如果不感兴趣可以跳过以下内容哦~~~~ package com.uiotsoft.daily.task;
import 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.ity.DailyHolidayConfig;
import com.ity.HolidayRawInfo;
import com.dule.service.DailyHolidayConfigService;
import com.dule.service.TaskService;
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 = wCall(request).execute();
rsa = response.body().string();
} catch (IOException e) {
e.printStackTrace();
}
Map map = JSONObject.parseObject(rsa, Map.class);
if (map != null) {
Integer code = (Integer) ("code");
if (code == 0) {
JSONObject holidayJson = (JSONObject) ("holiday");
String jsonString = JSONString();
log.info("获取节假⽇数据内容为 jsonString = 【{}】", jsonString);
Set<Map.Entry<String, Object>> entrySet = Set();
List<HolidayRawInfo> rawInfoList = new ArrayList<>();
for (Map.Entry<String, Object> entry : entrySet) {
String key = Key();
Object value = Value();
cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(value);
HolidayRawInfo holidayRawInfo = Bean(jsonObject, HolidayRawInfo.class);
rawInfoList.add(holidayRawInfo);
}
// 定义节假⽇集合
List<DailyHolidayConfig> holidayConfigList = new ArrayList<>();
for (HolidayRawInfo holidayRawInfo : rawInfoList) {
DailyHolidayConfig holidayConfig = new DailyHolidayConfig();
holidayConfig.Target());
holidayConfig.After());
holidayConfig.Date());
holidayConfig.Name());
holidayConfig.Rest());
holidayConfig.Wage());
holidayConfig.setCreateTime(new Date());
holidayConfigList.add(holidayConfig);
}
// 根据⽇期排序升序
List<DailyHolidayConfig> collect = holidayConfigList.stream().sorted(Comparatorparing(DailyHolidayConfig::getHolidayDate)).List()); // 批量插⼊节假⽇表中
holidayConfigService.insertBatch(collect);
} else {
<("E|NoSubmitTask|syncHoliday()|同步节假⽇信息时,调⽤节假⽇⽹站服务出错!");
}
}
log.info("每年12⽉28凌晨1点定时同步下⼀年的节假⽇信息,同步节假⽇结束时间 = {}", DateUtil.formatDateTime(new Date()));
}
}
到此这篇关于教你怎么⽤Java获取国家法定节假⽇的⽂章就介绍到这了,更多相关java获取国家法定节假⽇内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论