posted 12 years ago

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

http://www.coderanch.com/t/373812/java/java/java-util-Calendar-add-substract

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 ]

http://www.coderanch.com/t/373812/java/java/java-util-Calendar-add-substract

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

Posts: 1646

posted 12 years ago

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.

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

Ranch Hand

Posts: 8791

posted 12 years ago

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