This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Hands On Software Engineering with Python and have Brian Allbey on-line!
See this thread for details.
Win a copy of Hands On Software Engineering with Python this week in the Jython/Python forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Add days, but not weekend days  RSS feed

Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • 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
    • 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
    After some pecan pie, you might want to cleanse your palatte with this tiny ad:
    RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!