• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Calculate Business Days between two dates using java

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 28295
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22801
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Rob Spoor
Sheriff
Posts: 22801
131
Eclipse IDE Spring Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
The human mind is a dangerous plaything. This tiny ad is pretty safe:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic