Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to get National Holidays by Java Mini Program

2025-02-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to get national holidays with Java Mini Program". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "how Java Mini Program can achieve national holidays".

Preface

This holiday is strictly in accordance with the national requirements of the two-day off and statutory holidays and contains holiday make-up information, you can customize the processing according to your own needs.

The following is the Maven configuration, which is the dependency used by the program. If the version is available, you can use the latest one.

Maven configures com.squareup.okhttp okhttp ${okhttp.version} com.alibaba fastjson ${fastjson.version} Java program 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.*; / *

The TestDate class is used to:

*

@ author:hujm

*

Date: 17:43, April 22, 2021

*

@ remark:

* / 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)) } / * get weekends and holidays * * @ param year * @ param month * @ return * / public static Set JJR (int year, int month) {/ / get all weekend Set monthWekDay = getMonthWekDay (year, month); / / http://timor.tech/api/holiday api document address Map jjr = getJjr (year, month + 1) Integer code = (Integer) jjr.get ("code"); if (code! = 0) {return monthWekDay;} Map holiday = (Map) jjr.get ("holiday"); Set strings = holiday.keySet (); for (String str: strings) {Map stringObjectMap = holiday.get (str); Integer wage = (Integer) stringObjectMap.get ("wage") String date = (String) stringObjectMap.get ("date"); / / filter out if (wage.equals (1)) {monthWekDay.remove (date);} else {monthWekDay.add (date);}} return monthWekDay } / * * get holidays excluding weekends * * @ 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; / / decrypt data 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);} / * * get the weekend month starting from 0 * * @ param year * @ param mouth * @ return * / public static Set getMonthWekDay (int year, int mouth) {Set 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;}}

The above methods can be used immediately, and of course you can customize them according to your own needs.

The following are my own business requirements. Save the holiday information obtained by calling the API API to the local database. If you are not interested, you can skip the following.

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; / *

The NoSubmitTask class is used to:

*

@ author:hujm

*

Date: 17:10, April 16, 2021

*

@ remark:

* / @ Slf4j@Componentpublic class NoSubmitTask {@ Resource private DailyHolidayConfigService holidayConfigService; @ Value ("${syncAddress}") private String syncAddress; @ Scheduled (cron = "${syncHolidayDeadline}") public void syncHoliday () {log.info ("synchronize next year's holiday information at 1: 00 a.m. on December 28th every year, synchronize holiday start time = {}", DateUtil.formatDateTime (new Date ()); String url = syncAddress OkHttpClient client = new OkHttpClient (); Response response; / / decrypt data 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 ("get holiday data for jsonString = [{}]", jsonString); Set entrySet = holidayJson.entrySet (); List rawInfoList = new ArrayList () For (Map.Entry 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) } / / define holiday collection List 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) } / / sort by date in ascending order List collect = holidayConfigList.stream () .sorted (Comparator.comparing (DailyHolidayConfig::getHolidayDate)) .order (Collectors.toList ()); / / insert holidayConfigService.insertBatch (collect) in bulk into the holiday table } else {log.error ("E | NoSubmitTask | syncHoliday () | error calling holiday website service while synchronizing holiday information!") Log.info ("every year at 1: 00 a.m. on December 28th regularly synchronizes the holiday information of the following year, and the end time of holidays = {}", DateUtil.formatDateTime (new Date ();}} so far, I believe you have a better understanding of "how Java Mini Program can achieve access to national holidays", so you might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report