Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

Calculate number of days between two dates

Divya Gehlot
Ranch Hand
Posts: 243
Hi JavaRanchers!
I need to write method where I have to calculate number of working days between two dates and where these two dates are Date object coming from database.Can any one help me regarding this.

Dave Seligson
Greenhorn
Posts: 17
This should do it, though it's a little bit "brute-force-ish".
I assumed your non-work days were Saturday and Sunday.
I also assumed that the 2 dates you're passing in are relatively close to each other. For instance, if there were 10 years between the 2 dates, then this approach might be too inefficient.

In that case, I would lop off the beginning days until the first Monday, lop off the end days after the last Sunday, and use (genericDays % 7) * 5 (Five workdays per Seven weekdays) to figure out how many workdays are in the middle. Then I would still have to use the below code to add the workdays that occurred in the days that I lopped off at the start and finish.

Hope this helps,
Dave Seligson
SCJP, SCWCD

public static class WorkingDays {
public static int getWorkDays(Date startDt, Date endDt) {
Calendar startCal,endCal;
startCal = Calendar.getInstance();
startCal.setTime(startDt);
endCal = Calendar.getInstance();
endCal.setTime(endDt);
int workDays = 0;

//Return 0 if start and end are the same
if (startCal.getTimeInMillis()==endCal.getTimeInMillis()) {
return 0;
}
//Just in case the dates were transposed this prevents infinite loop
if (startCal.getTimeInMillis() > endCal.getTimeInMillis()) {
startCal.setTime(endDt);
endCal.setTime(startDt);
}

do {
if (startCal.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY && startCal.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
++workDays;
}
} while (startCal.getTimeInMillis() < endCal.getTimeInMillis() );

return workDays;
}
}

Divya Gehlot
Ranch Hand
Posts: 243
Hi,
Thanks for your help. But I need to include start and end date also in calulation of number of working days where we have holidays also including saturday and sundays and the two days can have diffrence of more than 5 years.Please help me out.

Dave Seligson
Greenhorn
Posts: 17
Hmmmm.
Ok, Well
1) It looks like you'll need some way to maintain a bunch of unique non-business dates. And you'll probably need a place to store these dates. And then, you'll need a way to retrieve and add dates to this place.
Some dates will have to be manually added, like holidays. What about weekends? How could you add all those? How about a utility function that just populates your db with every weekend date within your acceptable time frame's boundarys. (i.e. 5 years ago to 5 years in future?) Might as well sort this table by date.

2) Since you'll have a collection of non-work dates, and they're sorted, right, and you know the number of days between your start/end date, is there a way you could subtract the known non-work days from your total days? Maybe some SQL could help on this one?

Dave Seligson
SCJP, SCWCD

bart zagers
Ranch Hand
Posts: 234
• 1
It looks like this might be of any help. It tries to ease working with working-days and holidays. I have not yet used it myself though.

Also, when you do some non-trivial date manipulations, I would suggest you take a look at Joda-Time (if you haven't done so already). It makes your live so much easier.

Feroz Siddiqui
Greenhorn
Posts: 1
Divya Gehlot wrote:Hi,
Thanks for your help. But I need to include start and end date also in calulation of number of working days where we have holidays also including saturday and sundays and the two days can have diffrence of more than 5 years.Please help me out.

Hi Divya,

I understand your code but unable to run it properly can you write the full code so that i can implement it.

Thanks,
Feroz

Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15571
46
Feroz, welcome to the Ranch.

Please note that you are responding to a topic from 2007. I'm not sure that Divya is still here to read your question and answer it.

If you want help with this, then can you please in detail explain why you're unable to run it properly? What did you try and what problems or errors did you get?