• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Date function minus operation excluding weekends and holidays  RSS feed

 
Pratik Rudra
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ,

I need your help on the below implementation

I have deduct a number of days excluding holiday and weekends .So if I have to deduct 4 days from current day (02/12) and assuming 02/10 is a holiday my answer will be 02/05


Now below is the code I have come up with however its not working with the hardcoded dates I am passing in to the Holiday Calendar String .Only the last value is considered .

So could anyone please let me know how I should store this values and compare with the date


 
Tony Docherty
Bartender
Posts: 3271
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you have a method called findNextWorkingDay() which subtracts days, surely it should be called findPreviousWorkingDay() either that or you should be passing in a negative value to go backwards.

You can't just subtract a number of days from the current date and then check for weekends/holidays etc as you may have stepped over them. You need to repeatedly step one day at a time seeing if this new day is a day that should be counted until you have stepped the required number of working days.
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony is right. If you first write a "getNextWorkingDay()" method and a "getPreviousWorkingDay()" method, then when you want the date which is 4 working days back, you call getPreviousWorkingDay() 4 times.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pratik Rudra wrote:I have deduct a number of days excluding holiday and weekends .So if I have to deduct 4 days from current day (02/12) and assuming 02/10 is a holiday my answer will be 02/05.

I don't propose to go over all the good advice you've already been given, so I'll say this:

1. What you want to do is NOT simple. "Holidays" are notoriously complex, because they vary from country to country, and may include movable ones like Easter or Ramadan or Yom Kippur. Your idea of simply taking a set of dates is probably the easiest, but you need to ensure that the dates you have cover the calculation required - so, for example, if you want to deduct thirty "working" days from a January or February date, you better make sure you have dates for the previous year.

2. Dates are NOT Strings. I understand that you might want to supply them as Strings, but you need to convert them as soon as you possibly can. For more info, read the StringsAreBad page.

HIH

Winston
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:1. What you want to do is NOT simple. "Holidays" are notoriously complex, because they vary from country to country, and may include movable ones like Easter or Ramadan or Yom Kippur.

Winston is certainly correct, I can personally vouch for this having previously written a system that counted working days.

Leaving aside the really complex ones such as those Winston has already identified, there are many rules for those that appear to be the simple holidays such as these, many of which apply to UK bank holidays:

The nearest Monday to a given date.
The first/last Monday in a given month.
A given date or, if that date is not a working date the next working day to the given date.
Special rules where holidays collide with other holidays ie which one moves and where it moves to.
Some holidays are multiple day holidays which may need to wrap around a weekend or be fully in a week or just be on the next working days.
Special days that only appear in a specific year.
etc etc
 
Robert D. Smith
Ranch Hand
Posts: 221
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Warning the original poster about the inherent evils of calendar math is great and all, but let's not lose the forest here for the trees. It sounds like and I'm going to assume it is a class assignment, and for a programming exercise, we can safely overlook all the hidden monsters under the bed just waiting to jump out and bite us in our proverbial .....

Pratik, I would suggest you start looking in your switch's default segment, particularly the substring comands. I confess I haven't tried to compile your code, but stepping through it with my feeble mind, some of your calculations there seemed a bit off kilter. And I do also highly recommend you take up the other suggestions and create distinct getprevious and getnext day methods. Eliminates potential problems with signing.

Regards,
Robert
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robert D. Smith wrote:It sounds like and I'm going to assume it is a class assignment, and for a programming exercise, we can safely overlook all the hidden monsters under the bed just waiting to jump out and bite us in our proverbial .....

Whether true or not, trying to do this without converting the supplied Strings to a form that makes sense (ie, Dates) is making life difficult.

@Pratik: Calendar has a roll() method, which I think you might find very useful for solving your problem. I suggest you have a look at it.

Winston
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like Tony, I worked on a system where we needed to know "Next Working Day" and sometimes "Next Working Day After That". Our approach to figuring out what the holidays were was simple: We just asked the users to put their holiday dates for the upcoming year into a database table. Which is pretty much what Pratik has, if I'm not mistaken. So that part at least I'm in agreement with (except for specifying them as Strings).
 
Robert D. Smith
Ranch Hand
Posts: 221
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Robert D. Smith wrote:It sounds like and I'm going to assume it is a class assignment, and for a programming exercise, we can safely overlook all the hidden monsters under the bed just waiting to jump out and bite us in our proverbial .....

Whether true or not, trying to do this without converting the supplied Strings to a form that makes sense (ie, Dates) is making life difficult.

@Pratik: Calendar has a roll() method, which I think you might find very useful for solving your problem. I suggest you have a look at it.

Winston
I agree with this 100%, Winston. The point I was trying to make was to not needlessly burden the OP with information he didn't need for this particular assignment. Basing my assumption on this being a class assignment, I then assumed that he (my apologies, Pratik, but in the words of Dallas Corbin I speak two languages, english and bad English, so if I have the gender wrong I am truly sorry) would use local holidays. Any other interpretation would be due to my inability to clearly express an idea.

Regards,
Robert
 
Pratik Rudra
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the suggestions I have modified the code like below however I am not able to hardcode the dates properly

Any suggestion is appreciated

 
Robert D. Smith
Ranch Hand
Posts: 221
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean you are unable to hard code the dates properly? Can you show us any error messages or any output? What output were you expecting?

I would approach this in a couple of different ways.
Add some println statements in the substring code - see what you are passing in, and getting out.
drop the hard coded dates and put the data into a 2 dimension array of Integers.
move the substring code to its own method(s)

Regards,
Robert
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pratik Rudra wrote:Any suggestion is appreciated

First: My apologies; I steered you wrong with Calendar.roll(). It's not what you want, add() is just fine.

Second: You're pretty close, but you're still trying to deal with those holiday dates as Strings, which is making things unnecessarily complicated.

Assuming that your holidays are the same for every year and that the exercise is to count back n working days from a specified date. The way I would do it (in pseudo-code) is this:

1. Convert your holiday date strings to Java Calendars. The easiest way to do that is to just append a year (eg "/2004") to each one and use your DATE_FORMAT string. The year doesn't matter because you won't be using it in your calculation; although it might be prudent to make it a leap year for completeness.
2. Loop until you've found your n'th working day back pretty much as you are, subtracting 1 day from your startpoint each time, and skipping any days that are weekends OR any dates where the month and day of month are equal to any of your holiday dates. Personally, I'd write an isHoliday(Calendar) method to do this.

HIH

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!