File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Calculate number of days between two dates Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Calculate number of days between two dates " Watch "Calculate number of days between two dates " New topic
Author

Calculate number of days between two dates

Divya Gehlot
Ranch Hand

Joined: Sep 10, 2006
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.
Thanks in advance.


SCJP1.5(81%), SCDJWS(94%), next mission SCEA (but need to wait or that)
Dave Seligson
Greenhorn

Joined: Jul 12, 2007
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 {
startCal.add(Calendar.DAY_OF_MONTH, 1);
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

Joined: Sep 10, 2006
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.
Thnaks in advance.
Dave Seligson
Greenhorn

Joined: Jul 12, 2007
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

Joined: Feb 05, 2003
Posts: 234
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

Joined: Oct 11, 2010
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.
Thnaks in advance.


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

Joined: Aug 16, 2005
Posts: 14338
    
  22

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?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
 
subject: Calculate number of days between two dates