1. 程式人生 > 其它 >時間日期判斷之ChineseCalendarUtils日期日曆工具類

時間日期判斷之ChineseCalendarUtils日期日曆工具類

參考:

/**
* @author qyw
* @description 日期日曆工具類
* @date Created in 21:01 2019/1/31
**/
public class ChineseCalendarUtils {

// 法律規定的放假日期
// 此處寫死了。需要定時入庫再讀庫,政府每年都會在12月份左右公佈,無法提前預知
// todo db資料轉list,根據年份讀取
private List<String> lawHolidays = Arrays.asList("2017-12-30", "2017-12-31",
"2018-01-01", "2018-02-15", "2018-02-16", "2018-02-17", "2018-02-18",
"2018-02-19", "2018-02-20", "2018-02-21", "2018-04-05", "2018-04-06",
"2018-04-07", "2018-04-29", "2018-04-30", "2018-05-01", "2018-06-16",
"2018-06-17", "2018-06-18", "2018-09-22", "2018-09-23", "2018-09-24",
"2018-10-01", "2018-10-02", "2018-10-03", "2018-10-04", "2018-10-05",
"2018-10-06", "2018-10-07");
// 由於放假需要額外工作的週末
// todo db資料轉list,根據年份讀取
private List<String> extraWorkdays = Arrays.asList("2018-02-11", "2018-02-24",
"2018-04-08", "2018-04-28", "2018-09-29", "2018-09-30");

/**
* @author qyw
* @description 判斷是否是法定假日
* @date Created in 21:03 2019/1/31
**/
public boolean isLawHoliday(String calendar) throws Exception {
ChineseCalendarUtils.isValidDate(calendar);
if (lawHolidays.contains(calendar)) {
return true;
}
return false;
}

/**
* @author qyw
* @description 判斷是否是週末
* @date Created in 21:03 2019/1/31
**/
public boolean isWeekends(String calendar) throws Exception {
ChineseCalendarUtils.isValidDate(calendar);
// 先將字串型別的日期轉換為Calendar型別
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(calendar);
Calendar ca = Calendar.getInstance();
ca.setTime(date);
if (ca.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY
|| ca.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
return true;
}
return false;
}

/**
* @author qyw
* @description 判斷是否是需要額外補班的週末
* @date Created in 21:06 2019/1/31
**/
public boolean isExtraWorkday(String calendar) throws Exception {
ChineseCalendarUtils.isValidDate(calendar);
if (extraWorkdays.contains(calendar)) {
return true;
}
return false;
}

/**
* @author qyw
* @description 判斷是否是休息日(包含法定節假日和不需要補班的週末)
* @date Created in 21:06 2019/1/31
**/
public boolean isHoliday(String calendar) throws Exception {
ChineseCalendarUtils.isValidDate(calendar);
// 首先法定節假日必定是休息日
if (this.isLawHoliday(calendar)) {
return true;
}
// 排除法定節假日外的非週末必定是工作日
if (!this.isWeekends(calendar)) {
return false;
}
// 所有周末中只有非補班的才是休息日
if (this.isExtraWorkday(calendar)) {
return false;
}
return true;
}

/**
* @author qyw
* @description 校驗字串是否為指定的日期格式,含邏輯嚴格校驗,2007/02/30返回false
* @date Created in 21:06 2019/1/31
**/
private static boolean isValidDate(String str) {
boolean convertSuccess = true;
// 指定日期格式為四位年/兩位月份/兩位日期,注意yyyy-MM-dd區分大小寫;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
try {
// 設定lenient為false.
// 否則SimpleDateFormat會比較寬鬆地驗證日期,比如2007/02/29會被接受,並轉換成2007/03/01
format.setLenient(false);
format.parse(str);
} catch (ParseException e) {
convertSuccess = false;
}
return convertSuccess;
}


public static void main(String[] args) throws Exception {
String calendar = "2021-10-10";
ChineseCalendarUtils cc = new ChineseCalendarUtils();
System.out.println("輸入的calendar是否是指定要求的格式:"+ChineseCalendarUtils.isValidDate(calendar));
System.out.println("是否是法定節假日:" + cc.isLawHoliday(calendar));
System.out.println("是否是週末:" + cc.isWeekends(calendar));
System.out.println("是否是需要額外補班的週末:" + cc.isExtraWorkday(calendar));
System.out.println("是否是休息日:" + cc.isHoliday(calendar));
}
}

本文來自部落格園,作者:我是一個小倉鼠,轉載請註明原文連結:https://www.cnblogs.com/yongyuankuaile/p/15393125.html