輸出結果:
public static void main(String[] args) {
String sd = “2021-02-01”;
String ed = “2021-02-28”;
List list = getWorkDays(sd,ed);
for (String date: list) {
System.out.println(date);
}
}
/*** 此方法計算兩個日期的真實工作日(除周末和法定節假日)* @param startDate(起始日期),EndDate(結束時間)* @return list(去除周末和法定節假日后的日期字符串)*/
public static List<String> getWorkDays(String startDate,String EndDate){SimpleDateFormat simp = new SimpleDateFormat("yyyy-MM-dd");Date startTime = null;Date endTime = null;try {startTime = simp.parse(startDate);endTime = simp.parse(EndDate);} catch (ParseException e) {e.printStackTrace();}List<Date> datesBetweenTwoDate = getDatesBetweenTwoDate(startTime, endTime);List<String> list = new ArrayList<>();for (Date date : datesBetweenTwoDate) {String format = simp.format(date);list.add(format);}return list;
}/*** 判斷一個日期是不是周末.* @param calendar(要判斷的日期)* @return Boolean(返回true為是周末)*/
private static boolean isWeekend(Calendar calendar){//判斷是星期幾int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);if (dayOfWeek == 1 || dayOfWeek == 7){if(isWeekendWorkDays(calendar)){return false;}else{return true;}}return false;
}/*** 一個日歷是不是法定節假日* WorkDayPlusConfig.properties為配置文件,其中存放了法定節假日期,格式為:holiday=2018-10-1,...,..* @param calendar (要判斷的日期)* @return Boolean(返回true為是法定節假日)*/
private static boolean isHoliday(Calendar calendar){String pattern = "yyyy-MM-dd";SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);String dateString = simpleDateFormat.format(calendar.getTime());//節假日 這個可能不同地區,不同年份 都有可能不一樣,所以需要有個地方配置, 可以放數據庫, 配置文件,環境變量 等等地方//TODO 可以放到數據庫中String holidays = "2020-01-01,2020-01-24,2020-01-25,2020-01-26,2020-01-27,2020-01-28,2020-01-29,2020-01-30,2020-01-31," +"2020-02-01,2020-02-02," +"2020-04-04,2020-04-05,2020-04-06," +"2020-05-01,2020-05-02,2020-05-03,2020-05-04,2020-05-05," +"2020-06-25,2020-06-26,2020-06-27," +"2020-10-01,2020-10-02,2020-10-03,2020-10-04,2020-10-05,2020-10-06,2020-10-07,2020-10-08," +"2021-01-01,2021-01-02,2021-01-03,2021-02-11,2021-02-12," +"2021-02-13,2021-02-14,2021-02-15,2021-02-16,2021-02-17," +"2021-04-03,2021-04-04,2021-04-05,2021-05-01,2021-05-02," +"2021-05-03,2021-05-04,2021-05-05,2021-06-12,2021-06-13," +"2021-06-14,2021-09-19,2021-09-20,2021-09-21," +"2021-10-01,2021-10-02,2021-10-03,2021-10-04,2021-10-05,2021-10-06,2021-10-07";String[] holidayArray = holidays.split(",");boolean isHoliday = ArrayUtils.contains(holidayArray, dateString);return isHoliday;
}/*** 一個日歷是不是法定要工作的周末*WorkDayPlusConfig.properties為配置文件,其中存放了法定節假日期,格式為:WeekendWorkDays=2018-9-29,...,..* @param calendar (要判斷的日期)* @return Boolean(返回true為確實是要工作的周末)*/
private static boolean isWeekendWorkDays(Calendar calendar){String pattern = "yyyy-MM-dd";SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);String dateString = simpleDateFormat.format(calendar.getTime());//節假日 這個可能不同地區,不同年份 都有可能不一樣,所以需要有個地方配置, 可以放數據庫, 配置文件,環境變量 等等地方//TODO 可以放到數據庫中,節假日調的工作日,周末工作日String weekendWorkDays = "2020-01-19,2020-04-26,2020-05-09,2020-06-28,2020-09-27,2020-10-10,2021-02-07,2021-02-20,2021-04-25,2021-05-08,2021-09-18,2021-09-26,2021-10-09";String[] weekendWorkDaysArray = weekendWorkDays.split(",");boolean isWeekendWorkDays = ArrayUtils.contains(weekendWorkDaysArray, dateString);return isWeekendWorkDays;
}/*** 根據開始時間和結束時間返回時間段內的時間集合** @param beginDate* @param endDate* @return List*/
public static List<Date> getDatesBetweenTwoDate(Date beginDate, Date endDate) {Date workStartTime = endDate;//結束時間那天的上班時間List<Date> lDate = new ArrayList<Date>();Calendar cal = Calendar.getInstance();// 使用給定的 Date 設置此 Calendar 的時間cal.setTime(beginDate);boolean bContinue = true;while (bContinue) {//判斷是否到最后一天,結束循環if (endDate.after(cal.getTime()) || DateUtils.isSameDay(endDate, cal.getTime())) {if (!isWeekend(cal) && !isHoliday(cal)){lDate.add(cal.getTime());}} else {break;}// 日期加一cal.add(Calendar.DAY_OF_MONTH, 1);}if((!(DateUtils.isSameDay(beginDate, endDate)))&&endDate.after(workStartTime)&&endDate.after(beginDate)){//日期不是同一天且在當天工作時間(結束算起)之后lDate.add(endDate);// 把結束時間加入集合}return lDate;
}
總結
以上是生活随笔為你收集整理的Java 根据开始日期和结束日期,获取日期之间的工作日,去除了周末和法定节假日的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。