Wednesday, April 20, 2011

Date operation

Many time’s we need to get the next/ previous date of any particular date by discarding the Holidays (i.e. Saturday/Sunday).

I have some util method’s for your quick references :-

——————————————————————————————————–
1. Checking for holiday at the end of week for (T+1) :-
——————————————————————————————————–
public static Calendar getCobDateWithoutWeekendsForward( Date cobDate ) throws Exception
{
Calendar calObj = Calendar.getInstance();

try
{
calObj.setTime( cobDate );
int dayOfWeek = calObj.get(Calendar.DAY_OF_WEEK);

if(dayOfWeek == Calendar.FRIDAY)
{
calObj.add(Calendar.DATE, 3);
}
else
{
calObj.add(Calendar.DATE, 1);
}
}
catch(Exception exc)
{
throw new Exception( “Exception while getting cob date by filtering the week end in method
getCobDateWithoutWeekendsForward() :: ” + exc );
}
return calObj;
}

=================================================================================
2. Checking for holiday at the end of week for (T-1) :-
——————————————————————————————————–

public static Calendar getCobDateWithoutWeekendsBackword( Date cobDate ) throws Exception
{
Calendar calObj = Calendar.getInstance();

try
{
calObj.setTime( cobDate );
int dayOfWeek = calObj.get(Calendar.DAY_OF_WEEK);

if(dayOfWeek == Calendar.MONDAY)
{
calObj.add(Calendar.DATE, -3);
}
else
{
calObj.add(Calendar.DATE, -1);
}
}
catch(Exception exc)
{
throw new Exception( “Exception while getting cob date by filtering the week end in method
getCobDateWithoutWeekendsBackword() :: ” + exc );
}
return calObj;
}

No comments: