Let's say I have stored a bunch of holidays in my database.
What I need to do is find out which is next work day excluding Saturdays and the public holidays defined in the database.
eg.
Let's say today is the Friday the 15th Feb and the 17th and the 18th are public holidays as defined in the database as datetime. So now when I press a button that says next work day, it should return 19th Feb.
Which is the most efficient way to to get it?
Simplest.
Step 1: Get holidays from DB and format to your format, keep it in a List<String>
Step 2: Create a method that adds day.
public static Date addDays(Date d, int days) { Calendar cal = Calendar.getInstance(); cal.setTime(d); cal.add(Calendar.DATE, days); return cal.getTime(); }
Step 3: Create a method to find holiday.
public boolean isBankHoliday(java.util.Date d) {
Calendar c = new GregorianCalendar();
c.setTime(d);
if((Calendar.SATURDAY == c.get(c.DAY_OF_WEEK)) || (Calendar.SUNDAY == c.get(c.DAY_OF_WEEK)) || bankHolidays.contains(dString)) {
return (true);
} else {
return false;
}
}
Step 4: Get your input date. Check with bank holiday, loop until you find a working day.
while (isBankHoliday(myDate)) {
myDate = addDays(myDate, 1);
}