aspose file tools*
The moose likes Java in General and the fly likes Calculate Business Days between two dates using java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Calculate Business Days between two dates using java" Watch "Calculate Business Days between two dates using java" New topic
Author

Calculate Business Days between two dates using java

pallavi chaudhari
Ranch Hand

Joined: Jul 02, 2008
Posts: 54
Hi experts,

I want to find the business days between two dates. I found following solution on google.


Is it good way to calculate business days?

Suggest a solution

Thanks
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18662
    
    8

If it works for your test cases, then yes, it's a good way. But if it doesn't, then no, it isn't a good way.

What I'm saying: Set up some test cases and run the code to see if it produces the answer you want.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

I don't like the loop's guard. It's too precise. What if you want to calculate the difference in days between 2011-08-08 09:00 and 2011-08-08 18:00? Your code would result in one day, whereas it should be 0 days.

The following should be a better guard:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11419
    
  16

It is impossible to say if this is good or bad until you define what is a business day and what isn't. I work in healthcare, so EVERY day is a business day. Do you need to account for national holidays?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

I wrote an entire library at work (which means I can't share the code...) that can calculate the difference in both days and minutes between any two timestamps, including opening and closing times and holidays - all configurable of course. It wasn't easy though.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Calculate Business Days between two dates using java