aspose file tools*
The moose likes Java in General and the fly likes Date Function - HOLIDAY Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Date Function - HOLIDAY" Watch "Date Function - HOLIDAY" New topic
Author

Date Function - HOLIDAY

bob morkos
Ranch Hand

Joined: Oct 06, 2003
Posts: 56
How do I get the 2nd working day of a specific month and the last working day of a specific month. Can somoene help by providing some code or existant code. Thanks in advance.
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

I don't think there's any built-in java function like that. Particularly to take into accout holidays (holidays vary from country to country, state to state and even business to business -- i.e. I get off Martin Luther King Day, but my husband does not).
Can somoene help by providing some code or existant code.

also, realize we're not here to do your coding for you. We're here to help you to figure out how to do it yourself. So how about you try it 1st, and if you have problems -- explain what you've done, and what problem you encountered. That way you get the most out of the experience as well.
I think the API for the java.util.GregorianCalendar is going to be the best place to start. It will give you the ability to figure out what day a particular day is. I think you might need to hard code in holidays, or maybe list them in a properties file, in order to tell if a particular Monday is not a working day.
Good luck!


- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Tom Blough
Ranch Hand

Joined: Jul 31, 2003
Posts: 263
Bob,
For the second working day of a given month, create a Calendar object for the second day of the given month. If DAY_OF_WEEK is Saturday or Sunday use the add method to add 1 0r 2 days to the current value.
For the last working day of a specific month, get the first day of the specified month, add one month, then subtract one day. If the DAY_OF_WEEK is Saturday or Sunday add -1 or -2 days to get the last working day of the month.


Tom Blough<br /> <blockquote><font size="1" face="Verdana, Arial">quote:</font><hr>Cum catapultae proscriptae erunt tum soli proscripti catapultas habebunt.<hr></blockquote>
bob morkos
Ranch Hand

Joined: Oct 06, 2003
Posts: 56
Thanks for the tip. I will try it.
Originally posted by Tom Blough:

Bob,
For the second working day of a given month, create a Calendar object for the second day of the given month. If DAY_OF_WEEK is Saturday or Sunday use the add method to add 1 0r 2 days to the current value.
For the last working day of a specific month, get the first day of the specified month, add one month, then subtract one day. If the DAY_OF_WEEK is Saturday or Sunday add -1 or -2 days to get the last working day of the month.
bob morkos
Ranch Hand

Joined: Oct 06, 2003
Posts: 56
This is the method that I wrote to get the last day of the month. I don't know how to write a method to get the second working day of the month. The help that I need is to get the second working day of the month.

Originally posted by Tom Blough:
Bob,
For the second working day of a given month, create a Calendar object for the second day of the given month. If DAY_OF_WEEK is Saturday or Sunday use the add method to add 1 0r 2 days to the current value.
For the last working day of a specific month, get the first day of the specified month, add one month, then subtract one day. If the DAY_OF_WEEK is Saturday or Sunday add -1 or -2 days to get the last working day of the month.

[ Jess adjusted the <code> tags to [code] tags -- also I removed the
tags 'cause UBB has a bug when there's code and quote tags in the same post. ]
[ April 20, 2004: Message edited by: Jessica Sant ]
bob morkos
Ranch Hand

Joined: Oct 06, 2003
Posts: 56
Still having difficulty comming up with the second working day of a specific month.
To get Holiday Dates this is what I'm using:
http://www.javaworld.com/javatips/jw-javatip44_p.html

Originally posted by bob morkos:
This is the method that I wrote to get the last day of the month. I don't know how to write a method to get the second working day of the month. The help that I need is to get the second working day of the month.
<code>
public static int getLastDayOfMonth(int month, boolean isLeapYear) {
switch (month) {
case JANUARY :
return 31;
case FEBRUARY :
if (isLeapYear)
return 29;
else
return 28;
case MARCH :
return 31;
case APRIL :
return 30;
case MAY :
return 31;
case JUNE :
return 30;
case JULY :
return 31;
case AUGUST :
return 31;
case SEPTEMBER :
return 30;
case OCTOBER :
return 31;
case NOVEMBER :
return 30;
case DECEMBER :
return 31;
default :
return 0;
}
}
public static boolean isLastDayOfWorkOfMonth(Date aDate) {
Calendar c = Calendar.getInstance();
c.setTime(aDate);
/**
* If the day is a saturday or a sunday, it's obvious
* than we are not the last day!
*/
if (c.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY)
return false;
if (c.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
return false;
/**
* So check if tomorrow is the first of month
*/
c.add(Calendar.DAY_OF_MONTH, 1);
// We are friday? Add one more day to know if next
// monday will be the first of month.
if(c.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY)
c.add(Calendar.DAY_OF_MONTH, 1);
// If the saturday is the first, friday was the last day.
if (c.get(Calendar.DAY_OF_MONTH) == 1)
return true;
if(c.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
c.add(Calendar.DAY_OF_MONTH, 1);
if (c.get(Calendar.DAY_OF_MONTH) == 1)
return true;
return false;
}
<code>

Tom Blough
Ranch Hand

Joined: Jul 31, 2003
Posts: 263
As was previously mentioned, most ranchhands frown on posting code for solutions, but I had a few free minutes to code up the algorithm I previously presented. This is just a start, the class should really calculate the Nth working day of the month and not just the second and last. In addition, you'll need to take into account holidays for the current locale as was mentioned by Jessica.
Also, there is no need to re-invent the wheel. The Calendar class already takes care of leap years and the number of days in each month, so we let it handle those functions. That's why in my last day calculation, I set the date to the first of the subsequent month, since that is a day I know, then subtract one day from that. This automatically handles the days in each month as well as leap year. Your only worry now is holidays - the calendar object is like my employer, any day not a weekend is considered a work day ;-).

Here is the output:

[ April 21, 2004: Message edited by: Tom Blough ]
[ April 21, 2004: Message edited by: Tom Blough ]
bob morkos
Ranch Hand

Joined: Oct 06, 2003
Posts: 56
Thanks for the tip but it didn\t work for the month of August. Anyways, I corrected and it works fine now.

Originally posted by Tom Blough:
As was previously mentioned, most ranchhands frown on posting code for solutions, but I had a few free minutes to code up the algorithm I previously presented. This is just a start, the class should really calculate the Nth working day of the month and not just the second and last. In addition, you'll need to take into account holidays for the current locale as was mentioned by Jessica.
Also, there is no need to re-invent the wheel. The Calendar class already takes care of leap years and the number of days in each month, so we let it handle those functions. That's why in my last day calculation, I set the date to the first of the subsequent month, since that is a day I know, then subtract one day from that. This automatically handles the days in each month as well as leap year. Your only worry now is holidays - the calendar object is like my employer, any day not a weekend is considered a work day ;-).

Here is the output:

[ April 21, 2004: Message edited by: Tom Blough ]
[ April 21, 2004: Message edited by: Tom Blough ]
Tom Blough
Ranch Hand

Joined: Jul 31, 2003
Posts: 263
Yup, that's right. I misread it to be the second day of the month that was a working day.
Sorry.
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

nicely done folks -- glad you were able to figure it all out.
Bob -- in the future could you not "reply with quote" so much? it sure wastes a lot of space in the thread and kind of makes the conversation more difficult to follow.
Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Date Function - HOLIDAY