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

Add days, but not weekend days

 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There was a similar question, with an unhelpful resolution by the OP.
https://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 ]
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
     
    (instanceof Sidekick)
    Posts: 8791
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    We had to do something to compute n days in the future not counting weekends or holidays. The simplest solution was
    reply
      Bookmark Topic Watch Topic
    • New Topic