/**
* <p>Title: main </P>
* <p>Description: TODO </P>
* @param args
* return void 返回类型
* throws
* date 2014-11-24 上午09:11:47
*/
public static void main(String[] args) {
try {
List<String> dateStringList=new ArrayList<>();
dateStringList.add("2018-10-01");
dateStringList.add("2018-10-02");
dateStringList.add("2018-10-03");
dateStringList.add("2018-10-04");
dateStringList.add("2018-10-05");
dateStringList.add("2018-10-06");
dateStringList.add("2018-10-07");
List<String> specifiedDateList=new ArrayList<>();
specifiedDateList.add("2018-09-29");
specifiedDateList.add("2018-09-30");
initHolidayList(dateStringList,specifiedDateList);
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Calendar ca = Calendar.getInstance();
Date d = df.parse("2018-9-27");
ca.setTime(d);//设置当前时间
Test1 ct = new Test1();
String c = ct.addDateByWorkDay(ca,5);
System.out.println(c);
} catch ( Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
private static List<Calendar> holidayList = new ArrayList<Calendar>(); //节假日列表
private static List<Calendar> workList = new ArrayList<Calendar>(); //指定工作日期
/**
*
* <p>Title: addDateByWorkDay </P>
* <p>Description: TODO 计算相加day天,并且排除节假日和周末后的日期</P>
* @param calendar 当前的日期
* @param day 相加天数
* @return
* return Calendar 返回类型 返回相加day天,并且排除节假日和周末后的日期
* throws
* date 2014-11-24 上午10:32:55
*/
public static String addDateByWorkDay(Calendar calendar,int day){
try {
for (int i = 0; i < day; i++) {
calendar.add(Calendar.DAY_OF_MONTH, 1);
if(checkHoliday(calendar)){
i--;
}
}
} catch (Exception e) {
e.printStackTrace();
}
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
return df.format(calendar.getTime());
}
/**
*
* <p>Title: checkHoliday </P>
* <p>Description: TODO 验证日期是否是节假日</P>
* @param calendar 传入需要验证的日期
* @return
* return boolean 返回类型 返回true是节假日,返回false不是节假日
* throws
* date 2014-11-24 上午10:13:07
*/
public static boolean checkHoliday(Calendar calendar) throws Exception{
//判断日期是否是指定工作日
for (Calendar ca : workList) {
if(ca.get(Calendar.MONTH) == calendar.get(Calendar.MONTH) &&
ca.get(Calendar.DAY_OF_MONTH) == calendar.get(Calendar.DAY_OF_MONTH)&&
ca.get(Calendar.YEAR) == calendar.get(Calendar.YEAR)){
return false;
}
}
//判断日期是否是周六周日
if(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY ||
calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY){
return true;
}
//判断日期是否是节假日
for (Calendar ca : holidayList) {
if(ca.get(Calendar.MONTH) == calendar.get(Calendar.MONTH) &&
ca.get(Calendar.DAY_OF_MONTH) == calendar.get(Calendar.DAY_OF_MONTH)&&
ca.get(Calendar.YEAR) == calendar.get(Calendar.YEAR)){
return true;
}
}
return false;
}
/**
*
* <p>Title: initHolidayList </P>
* <p>Description: TODO 把所有节假日放入list,验证前要先执行这个方法</P>
* @param date 从数据库查 查出来的格式2014-05-09
* return void 返回类型
* throws
* date 2014-11-24 上午10:11:35
*/
public static void initHolidayList(List<String> holidayDateList,List<String> specifiedDateList) {
for (String string : holidayDateList) {
String[] da = string.split("-");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, Integer.valueOf(da[0]));
calendar.set(Calendar.MONTH, Integer.valueOf(da[1]) - 1);// 月份比正常小1,0代表一月
calendar.set(Calendar.DAY_OF_MONTH, Integer.valueOf(da[2]));
holidayList.add(calendar);
}
for (String string : specifiedDateList) {
String[] da = string.split("-");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, Integer.valueOf(da[0]));
calendar.set(Calendar.MONTH, Integer.valueOf(da[1]) - 1);// 月份比正常小1,0代表一月
calendar.set(Calendar.DAY_OF_MONTH, Integer.valueOf(da[2]));
workList.add(calendar);
}
}
initHolidayList(List<String> holidayDateList,List<String> specifiedDateList)
我在这个方法里面加了一个指定工作日参数把网上别人提供的优化了一下,节假日和指定工作日要新建一个数据表,去维护信息。
转载地址:https://blog.csdn.net/u011789653/article/details/41541555#commentBox