Java Calendar Holiday isHoliday(Calendar cal)

Here you can find the source of isHoliday(Calendar cal)

Description

Check if given Calendar object represents a holiday.

License

Open Source License

Parameter

Parameter Description
cal The Calendar to check.

Return

true if holiday, false otherwise.

Declaration

private static boolean isHoliday(Calendar cal) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;

import java.util.Map;
import java.util.Set;

public class Main {
    private static Map<Integer, Set<Date>> holidays;

    /**/*from  ww  w  .j av a  2s  . c o  m*/
     * Check if given Date object is a holiday.
     *
     * @param date
     *            The Date to check.
     * @return true if holiday, false otherwise.
     */
    public static boolean isHoliday(Date date) {
        return isHoliday(dateToCalendar(date));
    }

    /**
     * Check if given Calendar object represents a holiday.
     *
     * @param cal
     *            The Calendar to check.
     * @return true if holiday, false otherwise.
     */
    private static boolean isHoliday(Calendar cal) {
        int year = cal.get(Calendar.YEAR);
        Set<?> yearSet = getHolidaySet(year);
        for (Object aYearSet : yearSet) {
            Date date = (Date) aYearSet;
            if (checkDate(cal, dateToCalendar(date))) {
                return true;
            }
        }
        return false;
    }

    /**
     * Convert the given Date object to a Calendar instance.
     *
     * @param date
     *            The Date object.
     * @return The Calendar instance.
     */
    private static Calendar dateToCalendar(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal;
    }

    /**
     * Get a set of holidays for a given year.
     *
     * @param year
     *            The year to get holidays for.
     * @return The set of dates.
     */
    private static Set<Date> getHolidaySet(int year) {
        if (holidays == null) {
            holidays = new HashMap<Integer, Set<Date>>();
        }
        if (!holidays.containsKey(year)) {
            Set<Date> yearSet = new HashSet<Date>();

            // Add set holidays.
            yearSet.add(getDate(1, Calendar.JANUARY, year));
            yearSet.add(getDate(1, Calendar.MAY, year));
            yearSet.add(getDate(17, Calendar.MAY, year));
            yearSet.add(getDate(25, Calendar.DECEMBER, year));
            yearSet.add(getDate(26, Calendar.DECEMBER, year));

            // Add movable holidays - based on easter day.
            Calendar easterDay = dateToCalendar(getEasterDay(year));

            // Sunday before easter.
            yearSet.add(rollGetDate(easterDay, -7));

            // Thurday before easter.
            yearSet.add(rollGetDate(easterDay, -3));

            // Friday before easter.
            yearSet.add(rollGetDate(easterDay, -2));

            // Easter day.
            yearSet.add(easterDay.getTime());

            // Second easter day.
            yearSet.add(rollGetDate(easterDay, 1));

            // "Kristi himmelfart" day.
            yearSet.add(rollGetDate(easterDay, 39));

            // "Pinse" day.
            yearSet.add(rollGetDate(easterDay, 49));

            // Second "Pinse" day.
            yearSet.add(rollGetDate(easterDay, 50));

            holidays.put(year, yearSet);
        }
        return holidays.get(year);
    }

    /**
     * Check if the given dates match on day and month.
     *
     * @param cal
     *            The Calendar representing the first date.
     * @param other
     *            The Calendar representing the second date.
     * @return true if they match, false otherwise.
     */
    private static boolean checkDate(Calendar cal, Calendar other) {
        return checkDate(cal, other.get(Calendar.DATE),
                other.get(Calendar.MONTH));
    }

    /**
     * Check if the given date represents the given date and month.
     *
     * @param cal
     *            The Calendar object representing date to check.
     * @param date
     *            The date.
     * @param month
     *            The month.
     * @return true if they match, false otherwise.
     */
    private static boolean checkDate(Calendar cal, int date, int month) {
        return cal.get(Calendar.DATE) == date
                && cal.get(Calendar.MONTH) == month;
    }

    /**
     * Get the date for the given values.
     *
     * @param day
     *            The day.
     * @param month
     *            The month.
     * @param year
     *            The year.
     * @return The date represented by the given values.
     */
    private static Date getDate(int day, int month, int year) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month);
        cal.set(Calendar.DATE, day);
        return cal.getTime();
    }

    private static Date getEasterDay(int year) {
        int a = year % 19;
        int b = year / 100;
        int c = year % 100;
        int d = b / 4;
        int e = b % 4;
        int f = (b + 8) / 25;
        int g = (b - f + 1) / 3;
        int h = ((19 * a) + b - d - g + 15) % 30;
        int i = c / 4;
        int k = c % 4;
        int l = (32 + (2 * e) + (2 * i) - h - k) % 7;
        int m = (a + (11 * h) + (22 * l)) / 451;
        int n = (h + l - (7 * m) + 114) / 31; // This is the month number.
        int p = (h + l - (7 * m) + 114) % 31; // This is the date minus one.

        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, n - 1);
        cal.set(Calendar.DATE, p + 1);

        return cal.getTime();
    }

    /**
     * Add the given number of days to the calendar and convert to Date.
     *
     * @param calendar
     *            The calendar to add to.
     * @param days
     *            The number of days to add.
     * @return The date object given by the modified calendar.
     */
    private static Date rollGetDate(Calendar calendar, int days) {
        Calendar easterSunday = (Calendar) calendar.clone();
        easterSunday.add(Calendar.DATE, days);
        return easterSunday.getTime();
    }
}

Related

  1. getHolidays(Calendar beginDate, Calendar endDate)
  2. isHolidayInGermany(Calendar day)