java输入任意时间计算几天后时间(除节假日)
程序员文章站
2022-05-18 16:01:26
...
public String holidayDate(String date,Integer day) throws ParseException {
DateFormat format = new SimpleDateFormat("yyyyMM");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date newDate = new Date();
String yearMonth = format.format(newDate);
//获取节假日时间
List<String> list = configMapper.holidayList(yearMonth);
int ADay = day;
int actualDay = day;
for (int i= 1 ;i <= ADay;i++){
String date1 = date2(sdf.parse(date),i);
if(list != null && list.size()>0){
for (String s :list){
if (date1.equals(s)){
actualDay++;
ADay++;
}
}
}
}
String newDay = date(sdf.parse(date),actualDay);
return newDay;
}
private String date (Date date,int day){
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar c = Calendar.getInstance();
c.setTime(date);
// System.out.println("当前日期:"+sf.format(c.getTime()));
c.add(Calendar.DAY_OF_MONTH, day);
// System.out.println("增加一天后日期:"+sf.format(c.getTime()));
return sf.format(c.getTime());
}
private String date2 (Date date,int day){
SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.DAY_OF_MONTH, day);
return sf.format(c.getTime());
}
下一篇: leetcode 654