• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Calculate number of days between two dates

 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 234
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic