Java获取当月实际休息日(法定调休计算)

获取当月所有休息日 包含法定节假日和周六日

package com.mall.utils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

public class DateUtils {
public static void main(String[] args) {
System.out.println(loopAndWeek(2024, 10));
}

/**
*调用接口方法
* 
**/
public static String sendGet(String url, String param) {
	String result = "";
	BufferedReader in = null;
	try {
		String urlNameString = url + "/" + param;
		URL realUrl = new URL(urlNameString);
		URLConnection connection = realUrl.openConnection();
		connection.setRequestProperty("accept", "*/*");
		connection.setRequestProperty("connection", "Keep-Alive");
		connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
		connection.connect();
		in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
		String line;
		int cot = 0;
		while ((line = in.readLine()) != null) {
			result += line;
		}
	} catch (Exception e) {
		System.out.println("发送GET请求出现异常!" + e);
		e.printStackTrace();
	}
	finally {
		try {
			if (in != null) {
				in.close();
			}
		} catch (Exception e2) {
			e2.printStackTrace();
		}
	}
	return result;
}

/**
 * 获取某年 某月的休息日 法定 +  周六日  - 调休
 **/
public static Map<String,Object> loopAndWeek(Integer year, Integer month) {
	HashMap<String, Object> map1 = new HashMap<>();
	//法定节假日
	List<String> resultList = new ArrayList<>();
	//需要上班的休息日,调休日
	List<String> resultList2 = new ArrayList<>();
	String s = HttpUtil2.sendGet("https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query="+year+"%E5%B9%B4"+month+"%E6%9C%88&co=&resource_id=39043&t=1617089428269&ie=utf8&oe=gbk&cb=op_aladdin_callback&format=json&tn=wisetpl&cb=jQuery110203576901702188473_1617089118772&_=1617089118776", "");

	s = s.substring(s.indexOf("("));
	s = s.substring(1, s.length() - 2);
	Map<String, Object> map = (Map<String, Object>) JSONObject.parse(s);

	List list = (List) map.get("data");
	Map data = (Map) list.get(0);
	List<Map> almanac = (List<Map>) data.get("almanac");
	if(almanac == null || almanac.size() == 0) {
	}else {
		for(int i = 1; i < almanac.size(); i++) {
			String key = ((String)almanac.get(i).get("oDate")).substring(0, 10);
			String status = (String)almanac.get(i - 1).get("status");
			if(year.equals(Integer.parseInt(key.split("-")[0])) && month.equals(Integer.parseInt(key.split("-")[1]))) {
				if("1".equals(status)) {
					resultList.add(key);
				}
				if("2".equals(status)) {
					resultList2.add(key);
				}
			}
		}
	}
	//获取当前月所有周六周日
	List<String> weekends = getAllWeekends(year, month); // 例如获取2023年3月的所有周末
	for(String weekend : weekends) {
		//周末不包含调休日且周末不包含法定节假日
		if(!resultList2.contains(weekend) && !resultList.contains(weekend)) {
			resultList.add(weekend);
		}
	}
	LocalDate date = LocalDate.of(year, month, 1); // 该月的第一天
	LocalDate monthEnd = date.plusMonths(1); // 下个月的第一天,用于循环结束
	Integer num = 0;
	while (!date.isEqual(monthEnd)) {
		String format1 = date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
		if(!resultList.contains(format1)) {
			num++;
		}
		date = date.plusDays(1); // 增加一天
	}
	StringBuilder sb = new StringBuilder();
	if(CollectionUtil.isNotEmpty(resultList)) {
		resultList = resultList.stream().sorted().collect(Collectors.toList());
		resultList.forEach(e->{
			sb.append(e).append(",");
		});
	}
	//当月实际工时 人/天 hour=19
	map1.put("hour",num);
	//当月所有休息日 
	//示例:dayStr=2024-10-01,2024-10-02,2024-10-03,2024-10-04,2024-10-05,2024-10-06,2024-10-07,2024-10-13,2024-10-19,2024-10-20,2024-10-26,2024-10-27
	map1.put("dayStr", StringUtils.isNotBlank(sb.toString()) ? sb.toString().substring(0,sb.toString().length() - 1) : "");
	return map1;
}


/**
 * 获取某年某月的所有周六日
 **/
public static List<String> getAllWeekends(int year, int month) {
	List<String> weekends = new ArrayList<>();
	Calendar calendar = Calendar.getInstance();

	// 设置为指定的年月
	calendar.set(Calendar.YEAR, year);
	calendar.set(Calendar.MONTH, month - 1); // 月份从0开始
	calendar.set(Calendar.DAY_OF_MONTH, 1); // 设置为该月的第一天

	// 获取该月的第一天是周几
	int firstDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
	// 将calendar的日期设置到第一个周六,如果第一天就是周六,则添加到结果中
	if (firstDayOfWeek == Calendar.SATURDAY) {
		weekends.add(format.format(calendar.getTime()));
	}
	// 如果第一天就是周日,则添加到结果中
	if (firstDayOfWeek == Calendar.SUNDAY) {
		weekends.add(format.format(calendar.getTime()));
	}

	// 如果第一天不是周日,前进到下一周日
	if (firstDayOfWeek != Calendar.SUNDAY) {
		calendar.add(Calendar.DAY_OF_MONTH, Calendar.SUNDAY - firstDayOfWeek);


		calendar.add(Calendar.DAY_OF_MONTH,-1);
		int month1 = calendar.getTime().getMonth();
		if(month1+1 == month) {
			weekends.add(format.format(calendar.getTime()));
		}
		calendar.add(Calendar.DAY_OF_MONTH,1);
		int month2 = calendar.getTime().getMonth();
		if(month2+1 == month) {
			weekends.add(format.format(calendar.getTime()));
		}
	}

	// 循环直到下个月的第一天
	while (calendar.get(Calendar.MONTH) <= month - 1 || calendar.get(Calendar.YEAR) < year) {
		if(month == 12) {
			if(calendar.get(Calendar.YEAR) > year ) {
				break;
			}
		}
		// 默认已经在周日,所以只需要添加到下一个周六
		calendar.add(Calendar.DAY_OF_MONTH, 6);
		int month1 = calendar.getTime().getMonth();
		if(month1+1 == month) {
			weekends.add(format.format(calendar.getTime()));
		}
		calendar.add(Calendar.DAY_OF_MONTH, 1);
		int month2 = calendar.getTime().getMonth();
		if(month2+1 == month) {
			weekends.add(format.format(calendar.getTime()));
		}
	}

	return weekends;
}

}

参考连接 https://blog.csdn.net/nayi_224/article/details/109068470#_12

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值