Archive for the ‘Snippets’ Category
Calculate the Date n workings days ahead using Java
A little Java Snippet: Calculate the Date n workings days ahead:
/**
* Returns a new Date that is <code>workingDays</code> working days in the
* future based on <code>start</code>.
*
* @param start the base date
* @param workingDays count of working days
* @return newly created Date that is <code>workingDays</code> working days
* in the future based on <code>start</code>
*/
public static Date addWorkingDays(Date start, int workingDays) {
Calendar cal = Calendar.getInstance();
cal.setTime(start);
int base = workingDays + (cal.get(Calendar.DAY_OF_WEEK) – cal.get(Calendar.DAY_OF_MONTH) + 32) % 7;
cal.add(Calendar.DAY_OF_MONTH, base – 7 + (base + 4) / 5 * 2);
return cal.getTime();
}