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

Advertisements
This entry was posted in Java, Snippets. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s