Peter Fichtner's Blog

Archive for the ‘Snippets’ Category

Calculate the Date n workings days ahead using Java

leave a comment »

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();
}

Written by Peter Fichtner

March 10, 2010 at 1:31 pm

Posted in Java, Snippets

Follow

Get every new post delivered to your Inbox.