Here you can find the source of isBusinessDay(int dateIntToCheck)
private static boolean isBusinessDay(int dateIntToCheck)
//package com.java2s; import java.util.*; public class Main { private static final transient Map<Integer, List<Date>> computedDates = new HashMap<>(); private static HashMap<Integer, Boolean> ibdMemo = new HashMap<>(); private static boolean isBusinessDay(int dateIntToCheck) { Boolean result = ibdMemo.get(dateIntToCheck); if (result != null) return result; Date dateToCheck = convertToDate(dateIntToCheck); // Setup the calendar to have the start date truncated Calendar baseCal = Calendar.getInstance(); // baseCal.setTime(DateUtils.truncate(dateToCheck, Calendar.DATE)); // uses apache package--truncates dateToCheck to midnight of that date baseCal.setTime(dateToCheck);//from www .j av a 2 s. co m List<Date> offlimitDates; // Grab the list of dates for the year. These SHOULD NOT be modified. synchronized (computedDates) { int year = baseCal.get(Calendar.YEAR); // If the map doesn't already have the dates computed, create them. if (!computedDates.containsKey(year)) { computedDates.put(year, getOfflimitDates(year)); } offlimitDates = computedDates.get(year); } // Determine if the date is on a weekend. int dayOfWeek = baseCal.get(Calendar.DAY_OF_WEEK); boolean onWeekend = dayOfWeek == Calendar.SATURDAY || dayOfWeek == Calendar.SUNDAY; result = !(offlimitDates.contains(dateToCheck) || onWeekend); ibdMemo.put(dateIntToCheck, result); return result; } /** * converts dateInto to date * * @param dateInt dateInt to be converted * @return date */ public static Date convertToDate(int dateInt) { @SuppressWarnings("deprecation") Date nd = new Date(dateInt / 10000 - 1900, (dateInt / 100) % 100 - 1, dateInt % 100); return nd; } private static List<Date> getOfflimitDates(int year) { List<Date> offlimitDates = new ArrayList<>(); Calendar baseCalendar = GregorianCalendar.getInstance(); baseCalendar.clear(); // Add in the static dates for the year. // New years day baseCalendar.set(year, Calendar.JANUARY, 1); offlimitDates.add(offsetForWeekend(baseCalendar)); // Independence Day baseCalendar.set(year, Calendar.JULY, 4); offlimitDates.add(offsetForWeekend(baseCalendar)); // Veterans Day --not NYSE Holiday // baseCalendar.set(year, Calendar.NOVEMBER, 11); // offlimitDates.add(offsetForWeekend(baseCalendar)); // Christmas baseCalendar.set(year, Calendar.DECEMBER, 25); offlimitDates.add(offsetForWeekend(baseCalendar)); // Now deal with floating holidays. // Martin Luther King Day offlimitDates.add(calculateFloatingHoliday(3, Calendar.MONDAY, year, Calendar.JANUARY)); // Presidents Day offlimitDates.add(calculateFloatingHoliday(3, Calendar.MONDAY, year, Calendar.FEBRUARY)); // Memorial Day offlimitDates.add(calculateFloatingHoliday(0, Calendar.MONDAY, year, Calendar.MAY)); // Labor Day offlimitDates.add(calculateFloatingHoliday(1, Calendar.MONDAY, year, Calendar.SEPTEMBER)); // Columbus Day -- not NYSE Holiday // offlimitDates.add(calculateFloatingHoliday(2, Calendar.MONDAY, year, // Calendar.OCTOBER)); // Thanksgiving Day and Thanksgiving Friday -- Friday not NYSE Holiday Date thanksgiving = calculateFloatingHoliday(4, Calendar.THURSDAY, year, Calendar.NOVEMBER); offlimitDates.add(thanksgiving); // Friday after Thanksgiving is not NYSE Holiday // Good Friday Date goodFriday = GoodFridayObserved(year); offlimitDates.add(goodFriday); return offlimitDates; } private static Date offsetForWeekend(Calendar baseCal) { Date returnDate = baseCal.getTime(); if (baseCal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) { // if (log.isDebugEnabled()) { // log.debug("Offsetting the Saturday by -1: " + returnDate); // } return addDays(returnDate, -1); } else if (baseCal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) { // if (log.isDebugEnabled()) { // log.debug("Offsetting the Sunday by +1: " + returnDate); // } return addDays(returnDate, 1); } else { return returnDate; } } /** * This method will take in the various parameters and return a Date object * that represents that value. * * Ex. To get Martin Luther Kings BDay, which is the 3rd Monday of January, * the method call would be: * * calculateFloatingHoliday(3, Calendar.MONDAY, year, Calendar.JANUARY); * * Reference material can be found at: * http://michaelthompson.org/technikos/holidays.php#MemorialDay * * @param nth 0 for Last, 1 for 1st, 2 for 2nd, etc. * @param dayOfWeek Use Calendar.MONDAY, Calendar.TUESDAY, etc. * @param year year * @param month Use Calendar.JANUARY, etc. * @return date which corresponds to inputs */ private static Date calculateFloatingHoliday(int nth, int dayOfWeek, int year, int month) { Calendar baseCal = Calendar.getInstance(); baseCal.clear(); // Determine what the very earliest day this could occur. // If the value was 0 for the nth parameter, increment to the following // month so that it can be subtracted after. baseCal.set(year, month, 1); if (nth <= 0) baseCal.add(Calendar.MONTH, 1); Date baseDate = baseCal.getTime(); // Figure out which day of the week that this "earliest" could occur on // and then determine what the offset is for our day that we actually // need. int baseDayOfWeek = baseCal.get(Calendar.DAY_OF_WEEK); int fwd = dayOfWeek - baseDayOfWeek; // Based on the offset and the nth parameter, we are able to determine // the offset of days and then // adjust our base date. return addDays(baseDate, (fwd + (nth - (fwd >= 0 ? 1 : 0)) * 7)); } private static Date GoodFridayObserved(int nYear) { // Get Easter Sunday and subtract two days int nEasterMonth; int nEasterDay; int nGoodFridayMonth; int nGoodFridayDay; Date dEasterSunday; dEasterSunday = EasterSunday(nYear); GregorianCalendar gc = new GregorianCalendar(); gc.setTime(dEasterSunday); // nEasterMonth = dEasterSunday.getMonth(); nEasterMonth = gc.get(Calendar.MONTH); // nEasterDay = dEasterSunday.getDate(); nEasterDay = gc.get(Calendar.DAY_OF_MONTH); if (nEasterDay <= 3 && nEasterMonth == 3) // Check if <= April 3rd { switch (nEasterDay) { case 3: nGoodFridayMonth = nEasterMonth - 1; nGoodFridayDay = nEasterDay - 2; break; case 2: nGoodFridayMonth = nEasterMonth - 1; nGoodFridayDay = 31; break; case 1: nGoodFridayMonth = nEasterMonth - 1; nGoodFridayDay = 31; break; default: nGoodFridayMonth = nEasterMonth; nGoodFridayDay = nEasterDay - 2; } } else { nGoodFridayMonth = nEasterMonth; nGoodFridayDay = nEasterDay - 2; } // return new Date(nYear, nGoodFridayMonth, nGoodFridayDay); // old date // format return new GregorianCalendar(nYear, nGoodFridayMonth, nGoodFridayDay).getTime(); } /** * Add given number of days to a date. * * @param dateToAdd The date * @param numberOfDay The number of days to add * @return new date */ private static Date addDays(Date dateToAdd, int numberOfDay) { if (dateToAdd == null) { throw new IllegalArgumentException("Date can't be null!"); } Calendar tempCal = Calendar.getInstance(); tempCal.setTime(dateToAdd); tempCal.add(Calendar.DATE, numberOfDay); return tempCal.getTime(); } private static Date EasterSunday(int nYear) { /* * Calculate Easter Sunday * * Written by Gregory N. Mirsky * * Source: 2nd Edition by Peter Duffett-Smith. It was originally from * Butcher's Ecclesiastical Calendar, published in 1876. This algorithm * has also been published in the 1922 book General Astronomy by Spencer * Jones; in The Journal of the British Astronomical Association * (Vol.88, page 91, December 1977); and in Astronomical Algorithms * (1991) by Jean Meeus. * * This algorithm holds for any year in the Gregorian Calendar, which * (of course) means years including and after 1583. * * a=year%19 b=year/100 c=year%100 d=b/4 e=b%4 f=(b+8)/25 g=(b-f+1)/3 * h=(19*a+b-d-g+15)%30 i=c/4 k=c%4 l=(32+2*e+2*i-h-k)%7 * m=(a+11*h+22*l)/451 Easter Month =(h+l-7*m+114)/31 [3=March, 4=April] * p=(h+l-7*m+114)%31 Easter Date=p+1 (date in Easter Month) * * Note: Integer truncation is already factored into the calculations. * Using higher precision variables will cause inaccurate calculations. */ int nA; int nB; int nC; int nD; int nE; int nF; int nG; int nH; int nI; int nK; int nL; int nM; int nP; int nEasterMonth; int nEasterDay; if (nYear < 1900) { // if year is in java format put it into standard // format for the calculation nYear += 1900; } nA = nYear % 19; nB = nYear / 100; nC = nYear % 100; nD = nB / 4; nE = nB % 4; nF = (nB + 8) / 25; nG = (nB - nF + 1) / 3; nH = (19 * nA + nB - nD - nG + 15) % 30; nI = nC / 4; nK = nC % 4; nL = (32 + 2 * nE + 2 * nI - nH - nK) % 7; nM = (nA + 11 * nH + 22 * nL) / 451; // [3=March, 4=April] nEasterMonth = (nH + nL - 7 * nM + 114) / 31; --nEasterMonth; nP = (nH + nL - 7 * nM + 114) % 31; // Date in Easter Month. nEasterDay = nP + 1; // Un-correct our earlier correction. nYear -= 1900; // Populate the date object... // return new Date(nYear, nEasterMonth, nEasterDay); //old format for // date return new GregorianCalendar(nYear, nEasterMonth, nEasterDay).getTime(); } }