栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

获取所有节假日及周末

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

获取所有节假日及周末

在此感谢节假日API提供者。

实体(根据需求确认是否需要),需要重写hashCode、equals
import lombok.Data;

import java.time.LocalDateTime;


@Data
public class HolidayEntity {


    
    private Integer id;

    
    private String date;

    
    private String dateType;

    
    private String dateRemark;

    
    private Integer ofYear;

    
    private Integer status;

    
    private LocalDateTime createdDate;

    
    private String createdBy;

    
    private LocalDateTime updatedDate;

    
    private String updatedBy;


    
    @Override
    public boolean equals(Object obj) {
        // System.out.println("调用equals方法,当前的hashCode为:"+hashCode());
        
        if (obj == null) {
            return false;
        }
        
        if (this == obj) {
            return true;
        }
        
        if (obj instanceof HolidayEntity) {
            HolidayEntity vo = (HolidayEntity) obj;
            
            
            if (vo.date.equals(this.date)) {
                return true;
            }
        }
        return false;
    }

    
    @Override
    public int hashCode() {
        return this.getDate().hashCode() * this.getDate().hashCode();
    }
}
HolidayUtil
import cn.lin.entity.HolidayEntity;
import com.alibaba.fastjson.JSONObject;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

public class HolidayUtil {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        List holidayEntities = new ArrayList<>();
        for (int i = 1; i < 13; i++) {
            String format = new DecimalFormat("00").format(Integer.valueOf(i));
            Set holiday = getHoliday(year, format);
            holidayEntities.addAll(holiday);
        }
        System.out.println(1);
    }

	
    public static Set getHoliday(int year, String month){
        Set set = new HashSet<>();

        // 获取节假日不含周末------获取指定月份
        String url = "http://timor.tech/api/holiday/year/"+year+"-"+month;
        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();

        // 补班的日期
        List dateWorks = new ArrayList<>();
        try {
            response = client.newCall(request).execute();
            rsa = response.body().string();
            Map map = JSONObject.parseObject(rsa, Map.class);
            Map> holiday = (Map>) map.get("holiday");
            Set strings = holiday.keySet();
            for (String str : strings) {
                Map stringObjectMap = holiday.get(str);
                boolean bool = (Boolean) stringObjectMap.get("holiday");
                //筛选掉补班
                if (bool) {
                    String date = (String) stringObjectMap.get("date");
                    HolidayEntity holidayEntity = new HolidayEntity();
                    holidayEntity.setDate(date);
                    holidayEntity.setDateType("法定节假日");
                    holidayEntity.setDateRemark(String.valueOf(stringObjectMap.get("name")));
                    set.add(holidayEntity);
                } else {
                    String date = (String) stringObjectMap.get("date");
                    dateWorks.add(date);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 获取所有周末
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, Integer.valueOf(month) - 1);
        cal.set(Calendar.DATE, 1);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        while (cal.get(Calendar.YEAR) == year &&
                cal.get(Calendar.MONTH) < Integer.valueOf(month)) {
            int day = cal.get(Calendar.DAY_OF_WEEK);
            Date date = (Date)cal.getTime().clone();
            String time = simpleDateFormat.format(date);
            if (day == Calendar.SUNDAY && !dateWorks.contains(time)) { // 排除周末排班
                HolidayEntity holidayEntity = new HolidayEntity();
                holidayEntity.setDate(time);
                holidayEntity.setDateType("休息日");
                holidayEntity.setDateRemark("周日");
                set.add(holidayEntity);
            }
            if ( day == Calendar.SATURDAY && !dateWorks.contains(time)) { // 排除周末排班
                HolidayEntity holidayEntity = new HolidayEntity();
                holidayEntity.setDate(time);
                holidayEntity.setDateType("休息日");
                holidayEntity.setDateRemark("周六");
                set.add(holidayEntity);
            }
            cal.add(Calendar.DATE, 1);
        }

        return set;
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/930247.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号