java获取某年内工作日、休息日、节假日,返回json
程序员文章站
2022-03-03 10:02:59
...
直接上代码
package com.finesdk.util;
import com.alibaba.fastjson.JSON;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class WorkDayUtil {
public static class Day{
private String dayStr;
//类型 0代表上班 1周末休息 2节假日
private Integer type;
public String getDayStr() {
return dayStr;
}
public void setDayStr(String dayStr) {
this.dayStr = dayStr;
}
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
}
/**
* 获取某年内工作日、休息日、节假日
*
* @param year
* @return
*/
private static List<Day> getDates(int year, List<String> holiday, List<String> makeWork) {
List<Day> dates = new ArrayList<Day>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
for (int i = 1; i <= 12; i++){
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, i - 1);
cal.set(Calendar.DATE, 1);
while (cal.get(Calendar.YEAR) == year && cal.get(Calendar.MONTH) < i) {
int day = cal.get(Calendar.DAY_OF_WEEK);
if ((day == Calendar.SUNDAY || day == Calendar.SATURDAY)) {
Date work = (Date) cal.getTime().clone();
Day day1 = new Day();
day1.setDayStr(sdf.format(work));
day1.setType(1); //休息日
for (String hstr:holiday) {
if(hstr.equals(sdf.format(work))){
day1.setType(2); //节假日
}
}
for (String wStr:makeWork) {
if(wStr.equals(sdf.format(work))){
day1.setType(0); //补班
}
}
dates.add(day1);
}else{
Date work = (Date) cal.getTime().clone();
Day day1 = new Day();
day1.setDayStr(sdf.format(work));
day1.setType(0);
for (String hstr:holiday) {
if(hstr.equals(sdf.format(work))){
day1.setType(2);
}
}
dates.add(day1);
}
cal.add(Calendar.DATE, 1);
}
}
return dates;
}
public static void main(String[] args) {
//节假日
List<String> holiday = new ArrayList<>();
//补班
List<String> makeWork = new ArrayList<>();
//元旦
holiday.add("2020-01-01");
//春节
makeWork.add("2020-01-19");
holiday.add("2020-01-24");
holiday.add("2020-01-25");
holiday.add("2020-01-26");
holiday.add("2020-01-27");
holiday.add("2020-01-28");
holiday.add("2020-01-29");
holiday.add("2020-01-30");
holiday.add("2020-01-31");
holiday.add("2020-02-01");
holiday.add("2020-02-02");
//清明
holiday.add("2020-04-04");
holiday.add("2020-04-05");
holiday.add("2020-04-06");
//五一
makeWork.add("2020-04-26");
holiday.add("2020-05-01");
holiday.add("2020-05-02");
holiday.add("2020-05-03");
holiday.add("2020-05-04");
holiday.add("2020-05-05");
makeWork.add("2020-05-09");
//端午节
holiday.add("2020-06-25");
holiday.add("2020-06-26");
holiday.add("2020-06-27");
makeWork.add("2020-06-28");
//十一
makeWork.add("2020-09-27");
holiday.add("2020-10-01");
holiday.add("2020-10-02");
holiday.add("2020-10-03");
holiday.add("2020-10-04");
holiday.add("2020-10-05");
holiday.add("2020-10-06");
holiday.add("2020-10-07");
holiday.add("2020-10-08");
makeWork.add("2020-10-10");
//生成工作日、周末、节假日
List<Day> dates = getDates(2020,holiday,makeWork);
System.out.println(dates.size());
String s = JSON.toJSONString(dates);
System.out.println(s);
}
}
节假日holiday 和 补班makeWork 需要手动维护。
最后生成json数据如下图: