Post Reply 
 
Thread Rating:
  • 1 Votes - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Java Code to ignore holiday if the day is saturday / sunday
02-06-2011, 08:27 AM
Post: #1
Java Code to ignore holiday if the day is saturday / sunday
Java time code to ignore holiday ie saturday / sunday and return only the business day.The below example will explain in deatis.

This Code returns the calculated business day from the reference date i.e the day is not a saturday / sunday offset buy the value specified in the parameter.
If the offset = 0, it returns the next business day if the reference date is a holiday or else the same date.

If the offset is less than 0, it subtracts the offset # of business days from the reference date(the reference date will be shifted to the previous business date if its a holiday before the subtraction starts) and returns the calculated date .

If the offset is greater than 0, it adds the offset # of business days from the reference date(the reference date will be shifted to the next business date if its a holiday before the addition starts) and returns the calculated date.

import java.util.Calendar;
import java.util.Date;

public class GetBusinessDay {

public static void main(String[] args) throws Exception {

System.out.println(getNthBusinessDay(new Date(), -1));

}

public static Date getNthBusinessDay(Date referenceDate, int offset)
throws Exception {
Calendar objCalendar = Calendar.getInstance();
Date returnDate = null;
if (referenceDate != null) {
objCalendar.setTime(referenceDate);
if (offset == 0) {
if (objCalendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY)
objCalendar.add(Calendar.DATE, 2);
else if (objCalendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
objCalendar.add(Calendar.DATE, 1);
} else if (offset < 0) {
while (offset < 0) {
objCalendar.add(Calendar.DATE, -1);
if (!(objCalendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || objCalendar
.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY))
offset++;
}
} else if (offset > 0) {
while (offset > 0) {
objCalendar.add(Calendar.DATE, 1);
if (!(objCalendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || objCalendar
.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY))
offset--;
}
}
returnDate = objCalendar.getTime();
}
return returnDate;
}
}
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:



Send your comments, Suggestions or Queries regarding this site at info@tutorials4u.net

Copyright © 2010 Tutorials4u.net All Rights Reserved