File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Add days, but not weekend days Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Add days, but not weekend days" Watch "Add days, but not weekend days" New topic

Add days, but not weekend days

Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

There was a similar question, with an unhelpful resolution by the OP.

I'm trying to get the 'due date' of an item placed on order. So if the order is placed on day X, and it takes 'Y' days to produce, I don't want a simple X + Y calculation, because 1) the result might land on the weekend, and 2) the range might include the weekend.

My first (naieve) solution didn't take into account the 2nd problem. All I did was used Calendar.add, and checked the resulting days wasn't 1 or 7 (Sunday or Saturday in north america), then add 2 days. I didn't much care that 'Sunday' due times were being bumped ahead by an extra day.

I propose that one way to solve the 2nd problem is to 'walk' each day.
which produces:
Now, there's an outer for loop that lets me test things out, so that would be missing from the 'final' code. But.. it still seems a bit heavy-weight. Is there a quicker algorithm that can be done that just determines "from X date to Y date includes Z weekend days, therefore your result should be Y+Z, rather than Y".

[edited to fix code formatting]
[ October 15, 2004: Message edited by: Mike Curwen ]
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
There may indeed be an algorithm, but I bet you could figure one out with some thought.
  • X is the current date.
  • Y is the number of days to add to it.

  • First, every five days in the "delay" (Y) results in a full weekend being passed over. You're left, then, with figuring out whether or not adding the remainder (Z = Y mod 5 = 0..4) will pass a weekend. The answer depends on Z and the day of week that X falls on.

    For example, if X is a Wednesday and Z is 3 (Y is 3 or 8 or 13 or 18 ...), then a weekend will be passed. You should be able to create a simple hard-coded two-dimensional matrix of the number of days to add based on X's day of week and Z. The value at the intersection is 0, 1 or 2 depending on whether adding Z to X lands on a weekday, Sunday or Saturday, respectively.Oh, that's not entirely correct based on your problem. If the final date lands on a Sunday, from your description the date should be bumped to Tuesday as bumping it only to Monday means counting Saturday as a working day. Actually, all cases that pass a weekend should simply be bumped by two days.

    Thus, you only need to note in the matrix whether or not a weekend is passed or hit:And of course, looking at the pattern in the matrix screams "simplify!" -- it can be reduced to a simple expression: "boolean weekendHit = xDayOfWeek + zRemainder >= 7". Note that xDayOfWeek is in [1,7] (1 = Sunday, 7 = Saturday -- adjust if I'm mistaken) and zRemainder is in [0,4].

    I'll leave coallescing it into a final formula up to you.
    Stan James
    (instanceof Sidekick)
    Ranch Hand

    Joined: Jan 29, 2003
    Posts: 8791
    We had to do something to compute n days in the future not counting weekends or holidays. The simplest solution was

    A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
    I agree. Here's the link:
    subject: Add days, but not weekend days
    It's not a secret anymore!