my dog learned polymorphism
The moose likes Java in General and the fly likes Business days calculation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Business days calculation" Watch "Business days calculation" New topic

Business days calculation

Apu Nahasapeemapetilon
Ranch Hand

Joined: Sep 06, 2000
Posts: 51
We have a need for calculating the number of business days from a given date. Wait, that is ambiguous.
I pass you a date and an offset in days, you return the date that is the number of business days equal to the offset from the date passed. (example, date = friday 1/1/00 offset = 2, method would return Tuesday 1/5/00)
I can easily do this, but am wondering why they deprecated the date object with the calendar object and don't offer any business days functionality for the new calendar object? (isBusinessDay()???) Don't even get me started on holidays....
Anybody do this already?
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
Sorry no one else has replied - I don't know of anyplace that has this functionality already. As you say, it's not too difficult to write for yourself unless you want to include holidays as non-business days. The list of which holidays to include, and when they fall, will probably be location-specific and rather complicated to maintain. I suspect that you'll want to develop a method like
<code><pre> public boolean isBusinessDay(Date day) { ... }</pre></code>
which can tell you if a given date is considered a business day or not; you'll then have to call this function on each day in succession after the start date until you've found the required number of business days. The reason for this is that there are enough odd holidays whose dates change from year to year, that you really need to look up each day individually to see if it's a holiday. If all you wanted to do was skip the Saturdays and Sundays, there would be other ways to go about it, but once you include holidays there really won't be any shortcuts, I think. For holiday calculation, I think the easiest thing is to require someone to maintain a file which lists all the holidays within a given time period. Each time isBusinessDay() is called return false if it's a Saturday, Sunday, or is listed on the holiday list; true otherwise.
It does seem as though someone somewhere has probably developed something to do this, at least for "standard" US holidays (if that's where you are), but I have no idea where to find it.

"I'm not back." - Bill Harding, Twister
Steve Fahlbusch

Joined: Sep 18, 2000
Posts: 602

There is a java tip that discusses holidays (a trival activity to add a business day method).
Scroll down to resources, then click on tips index link
from index scroll down to tip # 44.
(full url)

but as a major aside, just what is a business day. i have created a lot of these functions over the years and it seems to have a different answer for every company i work for, consult to or teach.
hope this helps
I agree. Here's the link:
subject: Business days calculation
It's not a secret anymore!