ISSUE :
 
Profile photo
Submitted by Mark Mazerski
on October 16th, 2015 at 14:18:43 EST

Any algorithm to calculate US Federal holidays in JAVA as stated in OPM.GOV

Hi guys,
I am in process of implementing algorithm to calculate US federal holidays in JAVA.
Anyone has approached this problem or know any public API that I can use.
Thanks,
Mark


Profile photo
Replied by Yogaraj Khanal
on October 16th, 2015 at 14:21:01 EST

public static java.util.Calendar NewYearsDayObserved(int nYear) {
Calendar cal = new GregorianCalendar(nYear, Calendar.JANUARY, 1);
switch (cal.get(Calendar.DAY_OF_WEEK)) {
case Calendar.SATURDAY :
return (new GregorianCalendar(--nYear, Calendar.DECEMBER, 31));
case Calendar.SUNDAY :
return (new GregorianCalendar(nYear, Calendar.JANUARY, 2));
case Calendar.MONDAY :
case Calendar.TUESDAY :
case Calendar.WEDNESDAY :
case Calendar.THURSDAY :
case Calendar.FRIDAY :
default :
return cal;
}
}

public static java.util.Calendar MartinLutherKingObserved(int nYear) {
// Third Monday in January
Calendar cal = new GregorianCalendar(nYear, Calendar.JANUARY, 1);
switch(cal.get(Calendar.DAY_OF_WEEK))
{
case Calendar.SUNDAY :
return (new GregorianCalendar(nYear, Calendar.JANUARY, 16));
case Calendar.MONDAY :
return (new GregorianCalendar(nYear, Calendar.JANUARY, 15));
case Calendar.TUESDAY :
return (new GregorianCalendar(nYear, Calendar.JANUARY, 21));
case Calendar.WEDNESDAY :
return (new GregorianCalendar(nYear, Calendar.JANUARY, 20));
case Calendar.THURSDAY :
return (new GregorianCalendar(nYear, Calendar.JANUARY, 19));
case Calendar.FRIDAY :
return (new GregorianCalendar(nYear, Calendar.JANUARY, 18));
default : // Saturday
return (new GregorianCalendar(nYear, Calendar.JANUARY, 17));
}
}

public static java.util.Calendar PresidentsDayObserved(int nYear) {
// Third Monday in February
Calendar cal = new GregorianCalendar(nYear, Calendar.FEBRUARY, 1);
switch (cal.get(Calendar.DAY_OF_WEEK)) {
case Calendar.SUNDAY :
return (new GregorianCalendar(nYear, Calendar.FEBRUARY, 16));
case Calendar.MONDAY :
return (new GregorianCalendar(nYear, Calendar.FEBRUARY, 15));
case Calendar.TUESDAY :
return (new GregorianCalendar(nYear, Calendar.FEBRUARY, 21));
case Calendar.WEDNESDAY :
return (new GregorianCalendar(nYear, Calendar.FEBRUARY, 20));
case Calendar.THURSDAY :
return (new GregorianCalendar(nYear, Calendar.FEBRUARY, 19));
case Calendar.FRIDAY :
return (new GregorianCalendar(nYear, Calendar.FEBRUARY, 18));
default : // Saturday
return (new GregorianCalendar(nYear, Calendar.FEBRUARY, 17));
}
}

public static java.util.Calendar MemorialDayObserved(int nYear) {
// Last Monday in May
Calendar cal = new GregorianCalendar(nYear, Calendar.MAY, 1);
switch (cal.get(Calendar.DAY_OF_WEEK)) {
case Calendar.SUNDAY :
return (new GregorianCalendar(nYear, Calendar.MAY, 30));
case Calendar.MONDAY :
return (new GregorianCalendar(nYear, Calendar.MAY, 29));
case Calendar.TUESDAY :
return (new GregorianCalendar(nYear, Calendar.MAY, 28));
case Calendar.WEDNESDAY :
return (new GregorianCalendar(nYear, Calendar.MAY, 27));
case Calendar.THURSDAY :
return (new GregorianCalendar(nYear, Calendar.MAY, 26));
case Calendar.FRIDAY :
return (new GregorianCalendar(nYear, Calendar.MAY, 25));
default : // Saturday
return (new GregorianCalendar(nYear, Calendar.MAY, 31));
}
}

public static java.util.Calendar IndependenceDayObserved(int nYear) {
Calendar cal = new GregorianCalendar(nYear, Calendar.JULY, 4);
switch (cal.get(Calendar.DAY_OF_WEEK)) {
case Calendar.SATURDAY :
return (new GregorianCalendar(nYear, Calendar.JULY, 3));
case Calendar.SUNDAY :
return (new GregorianCalendar(nYear, Calendar.JULY, 5));
case Calendar.MONDAY :
case Calendar.TUESDAY :
case Calendar.WEDNESDAY :
case Calendar.THURSDAY :
case Calendar.FRIDAY :
default :
return cal;
}
}

public static java.util.Calendar LaborDayObserved (int nYear){
// The first Monday in September
Calendar cal = new GregorianCalendar(nYear, Calendar.SEPTEMBER, 1);
switch (cal.get(Calendar.DAY_OF_WEEK)) {
case Calendar.TUESDAY :
return (new GregorianCalendar(nYear, Calendar.SEPTEMBER, 7));
case Calendar.WEDNESDAY :
return (new GregorianCalendar(nYear, Calendar.SEPTEMBER, 6));
case Calendar.THURSDAY :
return (new GregorianCalendar(nYear, Calendar.SEPTEMBER, 5));
case Calendar.FRIDAY :
return (new GregorianCalendar(nYear, Calendar.SEPTEMBER, 4));
case Calendar.SATURDAY :
return (new GregorianCalendar(nYear, Calendar.SEPTEMBER, 3));
case Calendar.SUNDAY :
return (new GregorianCalendar(nYear, Calendar.SEPTEMBER, 2));
case Calendar.MONDAY :
default :
return cal;
}
}
Edited on October 16th, 2015 at 14:23:34 EST

Profile photo
Replied by Yogaraj Khanal
on October 16th, 2015 at 14:22:25 EST

public static java.util.Calendar ColumbusDayObserved (int nYear)
{
// Second Monday in October
Calendar cal = new GregorianCalendar(nYear, Calendar.OCTOBER, 1);
switch (cal.get(Calendar.DAY_OF_WEEK)) {
case Calendar.SUNDAY :
return (new GregorianCalendar(nYear, Calendar.OCTOBER, 9));
case Calendar.MONDAY :
return (new GregorianCalendar(nYear, Calendar.OCTOBER, 8));
case Calendar.TUESDAY :
return (new GregorianCalendar(nYear, Calendar.OCTOBER, 14));
case Calendar.WEDNESDAY :
return (new GregorianCalendar(nYear, Calendar.OCTOBER, 13));
case Calendar.THURSDAY :
return (new GregorianCalendar(nYear, Calendar.OCTOBER, 12));
case Calendar.FRIDAY :
return (new GregorianCalendar(nYear, Calendar.OCTOBER, 11));
default :
return (new GregorianCalendar(nYear, Calendar.OCTOBER, 10));
}

}
public static java.util.Calendar VeteransDayObserved(int nYear) {
// November 11th
Calendar cal = new GregorianCalendar(nYear, Calendar.NOVEMBER, 11);
switch (cal.get(Calendar.DAY_OF_WEEK)) {
case Calendar.SATURDAY :
return (new GregorianCalendar(nYear, Calendar.NOVEMBER, 10));
case Calendar.SUNDAY :
return (new GregorianCalendar(nYear, Calendar.NOVEMBER, 12));
case Calendar.MONDAY :
case Calendar.TUESDAY :
case Calendar.WEDNESDAY :
case Calendar.THURSDAY :
case Calendar.FRIDAY :
default :
return cal;
}
}
public static java.util.Calendar ThanksgivingObserved(int nYear) {
Calendar cal = new GregorianCalendar(nYear, Calendar.NOVEMBER, 1);
switch (cal.get(Calendar.DAY_OF_WEEK)) {
case Calendar.SUNDAY :
return (new GregorianCalendar(nYear, Calendar.NOVEMBER, 26));
case Calendar.MONDAY :
return (new GregorianCalendar(nYear, Calendar.NOVEMBER, 25));
case Calendar.TUESDAY :
return (new GregorianCalendar(nYear, Calendar.NOVEMBER, 24));
case Calendar.WEDNESDAY :
return (new GregorianCalendar(nYear, Calendar.NOVEMBER, 23));
case Calendar.THURSDAY :
return (new GregorianCalendar(nYear, Calendar.NOVEMBER, 22));
case Calendar.FRIDAY :
return (new GregorianCalendar(nYear, Calendar.NOVEMBER, 28));
default : // Saturday
return (new GregorianCalendar(nYear, Calendar.NOVEMBER, 27));
}
}
public static java.util.Calendar ChristmasDayObserved(int nYear) {
Calendar cal = new GregorianCalendar(nYear, Calendar.DECEMBER, 25);
switch (cal.get(Calendar.DAY_OF_WEEK)) {
case Calendar.SATURDAY :
return (new GregorianCalendar(nYear, Calendar.DECEMBER, 24));
case Calendar.SUNDAY :
return (new GregorianCalendar(nYear, Calendar.DECEMBER, 26));
case Calendar.MONDAY :
case Calendar.TUESDAY :
case Calendar.WEDNESDAY :
case Calendar.THURSDAY :
case Calendar.FRIDAY :
default :
return cal;
}
}