import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
public class GetFirstWorkDate {
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// holiday List for test
List<String> holidayList = new ArrayList<String>();
holidayList.add("2019-10-13");
holidayList.add("2019-10-14");
holidayList.add("2019-10-15");
// holidayList.add("2019-10-16");
// holidayList.add("2019-10-17");
holidayList.add("2019-10-21");
// start date
String date = "2019-10-07";
// first work day
Date scheduleActiveDate;
try {
scheduleActiveDate = getFirstWorkDate(holidayList, date);
System.out.println("first work day:" + sdf.format(scheduleActiveDate));
} catch (ParseException e) {
e.printStackTrace();
}
}
public static Date getFirstWorkDate(List<String> list, String date) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// init start date
Date currentDate = sdf.parse(date);
Date nextDate;
boolean delayFlg = false;
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("date", currentDate);
map.put("delayFlg", delayFlg);
// get the next work day
HashMap<String, Object> nextMap = getNextWorkDay(list, map);
// compare the next work day with start date,if they are the same
// day,then stop loop and return
while (!(((Date) map.get("date")).compareTo(((Date) nextMap.get("date"))) == 0)
|| ((((Date) map.get("date")).compareTo(((Date) nextMap.get("date"))) == 0
&& (boolean) nextMap.get("delayFlg")))) {
// reset the start date
nextDate = (Date) nextMap.get("date");
currentDate = (Date) map.get("date");
delayFlg = (boolean) nextMap.get("delayFlg");
if (currentDate.compareTo(nextDate) == 0 && delayFlg) {
nextDate = getTargetDaysLater(nextDate, 1);
delayFlg = false;
}
map.put("date", nextDate);
map.put("delayFlg", delayFlg);
// get the next work day
nextMap = getNextWorkDay(list, map);
}
return (Date) nextMap.get("date");
}
public static HashMap<String, Object> getNextWorkDay(List<String> list, HashMap<String, Object> map)
throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
HashMap<String, Object> mapTmp = new HashMap<String, Object>();
Date date = (Date) map.get("date");
boolean delayFlg = (boolean) map.get("delayFlg");
Calendar calendar = getCalendar(date);
if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
if (isHoliday(sdf.format(date), list)) {
delayFlg = true;
}
date = getTargetDaysLater(date, 1);
} else if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY
|| calendar.get(Calendar.DAY_OF_WEEK) == Calendar.TUESDAY
|| calendar.get(Calendar.DAY_OF_WEEK) == Calendar.WEDNESDAY
|| calendar.get(Calendar.DAY_OF_WEEK) == Calendar.THURSDAY) {
if (isHoliday(sdf.format(date), list)) {
date = getTargetDaysLater(date, 1);
}
} else if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY) {
if (isHoliday(sdf.format(date), list)) {
date = getTargetDaysLater(date, 1);
}
} else {
date = getTargetDaysLater(date, 1);
}
// System.out.println("getNextDay:" + sdf.format(date));
mapTmp.put("date", date);
mapTmp.put("delayFlg", delayFlg);
return mapTmp;
}
/**
* get the delayed date with given days
*
* @param Date
* start date
* @param int
* delay days
* @return
*/
public static Date getTargetDaysLater(Date date, int n) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, +n);
date = calendar.getTime();
return date;
}
/**
*
* @param date
* @return Calendar
* @throws ParseException
*/
public static Calendar getCalendar(Date date) throws ParseException {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal;
}
/**
*
* @param sdate
* @param list
* @return
* @throws ParseException
*/
public static boolean isHoliday(String sdate, List<String> list) throws ParseException {
if (list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
if (sdate.equals(list.get(i))) {
return true;
}
}
}
return false;
}
}
Recommended Posts