# computing week days between 2 dates

Can i get a code snippet for calculating / computing only week days (excluding saturday and sundays) between two dates (from and to date).

Thanks,
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
First You should use Calendar instead of Date:

Calendar cal = Calendar.getInstance().setTime(Date);

You can get week number of year:
cal.get(Calendar.WEEK_OF_YEAR);
day of week:
cal.get(Calendar.DAY_OF_WEEK);

and so on.

Having this, You can find full weeks passed, and find the days:

(fullweekcount - 1) * 5 + first week day count + last week day count
Author Commented:
can i get a complete code please...
Commented:
That will only work on days in the same year...
Commented:
If, when the end date is on a weekend, you don't want to count a day for it:

private long getWeekdayDifference(Date startDate, Date endDate) {
if (startDate.getTime() > endDate.getTime()) return -getWeekdayDifference(endDate, startDate);

Calendar cal = new GregorianCalendar();
cal.setTime(startDate);

long days = 0;
while (!cal.getTime().equals(endDate)) {
int weekDay = cal.get(Calendar.DAY_OF_WEEK);
if (weekDay != Calendar.SATURDAY && weekDay != Calendar.SUNDAY)
days++;

}
return days;
}

If, when the end date is on a weekend, you DO want to count 1 day (as if it were the following monday):

private long getWeekdayDifference(Date startDate, Date endDate) {
if (startDate.getTime() > endDate.getTime()) return -getWeekdayDifference(endDate, startDate);

Calendar cal = new GregorianCalendar();
cal.setTime(startDate);

long days = 0;
while (!cal.getTime().equals(endDate)) {
int weekDay = cal.get(Calendar.DAY_OF_WEEK);
if (weekDay != Calendar.SATURDAY && weekDay != Calendar.SUNDAY)
days++;
}
return days;
}
Commented:
that is, from a friday to the following saturday, if you want to count that as 1, use the second one, otherwise use the first.
Author Commented:
Thanks dberner9.
Your code works fine for me...

I need one more favour.. I need to find end date from given from date which contains 100 working days.

Ex: If start date given is 05/01/2005, end date should be calculated as 09/19/2005.

Can i also get a code snippet for this.
Commented:
private long getEndDate(Date startDate, long businessDays) {
if (startDate.getTime() > endDate.getTime()) return -getWeekdayDifference(endDate, startDate);

Calendar cal = new GregorianCalendar();
cal.setTime(startDate);

long days = 0;
for (long i = 0; i < businessDays; i++) {
int weekDay = cal.get(Calendar.DAY_OF_WEEK);
if (weekDay == Calendar.SATURDAY || weekDay == Calendar.SUNDAY) i--;
}
return cal.getTime();
}
Commented:
private long getEndDate(Date startDate, long businessDays) {
Calendar cal = new GregorianCalendar();
cal.setTime(startDate);

long days = 0;
for (long i = 0; i < businessDays; i++) {
int weekDay = cal.get(Calendar.DAY_OF_WEEK);
if (weekDay == Calendar.SATURDAY || weekDay == Calendar.SUNDAY) i--;
}
return cal.getTime();
}

Experts Exchange Solution brought to you by