java⾃动⽣成⼀年中⼯作⽇和休息⽇(公休⽇及假期国家法定假⽇)
说明
计算周期为每年的12⽉1号⾄下⼀年的12⽉之前(即12⽉1号⽣成下⼀年的假期,因为⼀次性⽣成,避开⾼并发时段,适合12⽉1号凌晨执⾏)。
本计算⽅法⼀共基于两个类实现,⼀个计算类VacationDayCalculate,⼀个⽇历类Lunar(还有⼀个⽇期辅助处理类DataUtils)代码注释较全,请注意查看有助于理解,main⽅法在计算类VacationDayCalculate最下⾯,可运⾏测试。
计算类VacationDayCalculate
ParseException;
SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
王姬个人资料import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;
/**
* 从12⽉1号起计算⼀年中的假期
*/
public class VacationDayCalculate {
/
/ 3天节假⽇规则=节⽇在周⼏,放假⽇在周⼏_放假之前的多少天上班_放假后的多少天上班;
private String vacationWeek ="1,6-7-1_0_0;2,7-1-2_1_0;3,1-2-3_2_0;4,4-5-6_0_1;5,5-6-7_0_0;6,6-7-1_0_0;7,6-7-1_0_0";
// 7天节假⽇规则=节⽇在周⼏,放假之前的多少天上班_放假后的多少天上班;
private String vacationMax ="1,2_0;2,2_0;3,3_0;4,0_3;5,0_2;6,0_2;7,1_1";
/**
* ⼀个时间集合,放假则为true,⼯作⽇为false 放假包括国家法定节假⽇和双休⽇起诉费用
*/
public HashMap<String, Boolean>yearVacationDay(Integer year){
HashMap<String, Boolean> dates =weekVacation(year -1);
// 3天假⽇在周⼏放假规则
HashMap<Integer, String> weeks =new HashMap<>();
String[] weeksTemp = vacationWeek.split(";");
for(String weekStr : weeksTemp){
String[] week = weekStr.split(",");
weeks.put(Integer.parseInt(week[0]), week[1]);
}
// 元旦节公历⼀⽉⼀⽇,放假3天
String vacationDay = year +"-01-01";
setVacationThreeDay(vacationDay, dates, weeks);
// 清明节(不分⽇历,有规律)年份能整除4的年和下⼀年为4⽉4⽇,再下两年为4⽉5⽇,放假3天
int temp = year &3;
if(temp <2){
vacationDay = year +"-04-04";
}else{
vacationDay = year +"-04-05";
}
// 劳动节公历五⽉⼀⽇,放假⼀天,没有倒休
dates.put("05-01",true);
// 端午节农历五⽉初五,放假3天
vacationDay =String(),5,5);
setVacationThreeDay(vacationDay, dates, weeks);
// 中秋节农历⼋⽉⼗五,放假3天
vacationDay =String(),8,15);
setVacationThreeDay(vacationDay, dates, weeks);
// 7天假⽇在周⼏放假规则
weeks.clear();
weeksTemp = vacationMax.split(";");
for(String weekStr : weeksTemp){
String[] week = weekStr.split(",");
weeks.put(Integer.parseInt(week[0]), week[1]);
}
// 春节农历⼀⽉初⼀,放假从前⼀天除⼣开始,放假7天
vacationDay =String(),1,1);
SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd");
try{
Long time = format.parse(vacationDay).getTime()-86399000;
vacationDay = format.format(time);
}catch(ParseException e){
e.printStackTrace();
}
setVacationSevenDay(vacationDay, dates, weeks);
// 国庆节公历⼗⽉⼀号,放假7天
vacationDay = year +"-10-01";
setVacationSevenDay(vacationDay, dates, weeks);
return dates;
}
/**
* 从21⽉1⽇起放⼊之后⼀年的时间注:可通过代码的注释和释放选择是否加⼊双休⽇
* @return map集合,key为⽇期(不包括年),value是true为休息⽇,false为⼯作⽇
*/
private HashMap<String, Boolean>weekVacation(Integer year){
// 放⼊⼀年的时间
HashMap<String, Boolean> dates =new LinkedHashMap<>();
Calendar cal = Instance();
cal.DateByString(year +"-12-1 12:00:00","yyyy-MM-dd hh:mm:ss"));
Integer days =365;
if((year &3)==0){
days =366;
}
for(int i =0, j =0; i < days; i++, j =1){
cal.add(Calendar.DAY_OF_YEAR, j);
String date = Time(),"MM-dd");
// 这⾥加⼊双休
// Integer ifVacation = Time().getTime());
// if (ifVacation == 6 || ifVacation == 7) {
// dates.put(date, true);
// } else {
/
/ dates.put(date, false);
// }
// 若不需要双休,只需要法定假⽇,则将上⾯的⼏⾏注释掉,放开下⾯这⼀⾏
dates.put(date,false);
韩国歌手具俊晔}
* 3天假期计算放假⽇期和上班⽇期并修改
* @param vacationDay 节⽇
* @param dates 时间集合
* @param weeks 放假周规律集合
*/
private void setVacationThreeDay(String vacationDay, HashMap<String, Boolean> dates, HashMap<Integer, String> weeks){
Integer week =dayForWeek(vacationDay);
String[] vacation = (week).split("_");
int indexOf = vacation[0].String());
Integer[] interval =dayForWeekThree(indexOf);
Integer incr = Integer.parseInt(vacation[1]);
Integer decr = Integer.parseInt(vacation[2]);
List<String> vacationDate =dayForWeek(vacationDay, interval[0], interval[1]);
for(String day : vacationDate){
dates.put(day,true);
}
List<String> workDate =dayForWork(vacationDay, interval[0], interval[1], incr, decr);
for(String day : workDate){
dates.put(day,false);
}
}
/**
* 7天假期计算放假⽇期和上班⽇期并修改
* @param vacationDay 节⽇
* @param dates 时间集合
* @param weeks 放假周规律集合
*/
private void setVacationSevenDay(String vacationDay, HashMap<String, Boolean> dates, HashMap<Integer, String> weeks){
Integer week =dayForWeek(vacationDay);
String[] vacation = (week).split("_");
Integer incr = Integer.parseInt(vacation[0]);
Integer decr = Integer.parseInt(vacation[1]);
List<String> vacationDate =dayForWeek(vacationDay,0,6);
for(String day : vacationDate){
dates.put(day,true);
渴望力}
List<String> workDate =dayForWork(vacationDay,0,6, incr, decr);
for(String day : workDate){
dates.put(day,false);
}
}
/**
* 获得指定字符串的时间是星期⼏
* @param pTime 时间字符串
*/
private Integer dayForWeek(String pTime){
SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Instance();
try{
Date tmpDate = format.parse(pTime);
cal.setTime(tmpDate);
}catch(Exception e){
e.printStackTrace();
}
int w = (Calendar.DAY_OF_WEEK)-1;// 指⽰⼀个星期中的某天。
if(w <0)
w =0;
if(w ==0)
w =7;
* 获得指定毫秒数的时间是星期⼏
* @param pTime 时间字符串
*/
private Integer dayForWeek(Long pTime){
Calendar cal = Instance();
try{
cal.setTime(new Date(pTime));
}catch(Exception e){
e.printStackTrace();
}
int w = (Calendar.DAY_OF_WEEK)-1;// 指⽰⼀个星期中的某天。if(w <0)
w =0;
if(w ==0)
w =7;
return w;
}
/**
* 放假三天的假⽇放假时间
* @param indexOf 节⽇在周⼏
* @return 放假时间范围在节⽇之前开始和⼏⽇之后结束
*/
private Integer[]dayForWeekThree(Integer indexOf){
Integer incr, decr;
if(indexOf ==0){
incr =0;
decr =2;
}else if(indexOf ==2){
incr =1;
decr =1;
}else{
incr =2;
decr =0;
}
return new Integer[]{ incr, decr };
}
/
**
* 获得指定时间前⼏天的⽇期或者后⼏天的⽇期
* @param pTime 时间
* @param incr 之前⼏天
* @param decr 之后⼏天
* @return 放假时间⽇期集合
*/
private List<String>dayForWeek(String pTime, Integer incr, Integer decr){ SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat rformat =new SimpleDateFormat("MM-dd");
List<String> result =new ArrayList<>(10);
Calendar cal = Instance();
try{
Date tmpDate = format.parse(pTime);
result.add(rformat.format(tmpDate));
cal.setTime(tmpDate);
}catch(Exception e){
e.printStackTrace();谢娜张杰个人资料
}
for(int i =0; i < incr; i++){
cal.add(Calendar.DAY_OF_YEAR,-1);
result.add(rformat.Time()));
}
cal.add(Calendar.DAY_OF_YEAR,1* incr);
for(int i =0; i < decr; i++){
cal.add(Calendar.DAY_OF_YEAR,1);
result.add(rformat.Time()));
}
return result;
}
/**
* 获得放假之前和之后需要上班的时间
* @param pTime 节⽇时间
* @param v_incr 节⽇之前⼏天开始放假
* @param v_decr 节⽇之后⼏天开始放假
* @param w_incr 节⽇之前⼯作⼏天
* @param w_decr 节⽇之后⼯作⼏天
* @return 节假⽇前后需要上班的周六⽇时间
*/
private List<String>dayForWork(String pTime, Integer v_incr, Integer v_decr, Integer w_incr, Integer w_decr){ SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat rformat =new SimpleDateFormat("MM-dd");
Calendar cal = Instance();
try{
Date tmpDate = format.parse(pTime);
cal.setTime(tmpDate);
}catch(Exception e){
e.printStackTrace();
}
List<String> result =new ArrayList<>(5);
cal.add(Calendar.DAY_OF_YEAR, v_incr *-1);
for(int i =0; i < w_incr; i++){
四级写作和翻译多少分cal.add(Calendar.DAY_OF_YEAR,-1);
result.add(rformat.Time()));
}
cal.add(Calendar.DAY_OF_YEAR, v_incr + w_incr + v_decr);
for(int i =0; i < w_decr; i++){
cal.add(Calendar.DAY_OF_YEAR,1);
result.add(rformat.Time()));
}
return result;
}
/**
* 根据传⼊的农历⽇期计算公历⽇期
* @param year 年
* @param month ⽉
* @param day ⽇
* @return "年-⽉-⽇"
*/
public String lunar(String year, Integer month, Integer day){
Calendar cal = Instance();
SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
try{
cal.setTime(sdf.parse(year +"-"+ month +"-"+ day));
}catch(ParseException e){
e.printStackTrace();
}
calcuLunar(cal, month, day,new Lunar(true));
return year +"-"+((Calendar.MONTH)+1)+"-"+ (Calendar.DAY_OF_MONTH);
}
/**
* 农历转公历计算
* @param cal ⽇期对象
* @param month ⽉
* @param day ⽇
发布评论