Thursday, June 13, 2019

Java Program to check federal holidays in US using ZonedDateTime API java8

This is the program n=in java where it checks whether the given zoneddatetime is holidays or not.
It uses java8 ZonedDateTime to determine the zoned date and time.


Code to check whether a given date falls on weekend or holidays
public static boolean isHolidays(ZonedDateTime zdt){
Month month = zdt.getMonth();
int date = zdt.getDayOfMonth();
DayOfWeek day = zdt.getDayOfWeek();
// check if New Year's Day
if(month.equals(Month.JANUARY) && date == 1){
return true;
}
if(month.equals(Month.JANUARY) &&
date == 31 &&
day.equals(DayOfWeek.FRIDAY) ){
return true;
}
if(month.equals(Month.JANUARY)
&& date == 2 &&
day.equals(DayOfWeek.MONDAY) ){
return true;
}
// check MLK Day (3rd Monday of January)
if (month.equals(Month.JANUARY)
&& zdt.get(ChronoField.ALIGNED_WEEK_OF_MONTH) == 3
&& day.equals(DayOfWeek.MONDAY)) {
return true;
}
//// check Memorial Day (last Monday of May)
if(month.equals(Month.MAY)
&& day.equals(DayOfWeek.MONDAY)
&& date > (31-7)) {
return true;
}
// check if 4th of July
if(month.equals(Month.JULY)
&& date == 4){
return true;
}
if(month.equals(Month.JULY)
&& date == 3
&& day.equals(DayOfWeek.FRIDAY) ) {
return true;
}
if(month.equals(Month.JULY)
&& date == 5
&& day.equals(DayOfWeek.MONDAY) ){
return true;
}
// check Labor Day (1st Monday of September)
if (month.equals(Month.SEPTEMBER)
&& zdt.get(ChronoField.ALIGNED_WEEK_OF_MONTH) == 1
&& day.equals(DayOfWeek.MONDAY)) {
return true;
}
// check if Christmas
if(month.equals(Month.DECEMBER)
&& date == 25){
return true;
}
if(month.equals(Month.DECEMBER)
&& date == 24
&& day.equals(DayOfWeek.FRIDAY) ){
return true;
}
if(month.equals(Month.DECEMBER)
&& date == 26
&& day.equals(DayOfWeek.FRIDAY) ){
return true;
}
// check Thanksgiving (4th Thursday of November)
if (month.equals(Month.NOVEMBER)
&& zdt.get(ChronoField.ALIGNED_WEEK_OF_MONTH) == 4
&& day.equals(DayOfWeek.THURSDAY)) {
return true;
}
//check Thanksgiving following friday
if (month.equals(Month.NOVEMBER)
&& zdt.get(ChronoField.ALIGNED_WEEK_OF_MONTH) == 4
&& day.equals(DayOfWeek.FRIDAY)) {
return true;
}
//edge case when following day is 29 won't fall in align week 4
if (month.equals(Month.NOVEMBER)
&& zdt.get(ChronoField.ALIGNED_WEEK_OF_MONTH) == 5
&& date == 29
&& day.equals(DayOfWeek.FRIDAY)) {
return true;
}
return false;
}

No comments:

Post a Comment