Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

import java.util.Arrays;
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;

    /**
     * Return a sorted array of holidays for a given year.
     *
     * @param year
     *            The year to get holidays for.
     * @return The array of holidays, sorted by date.
     */
    public static Date[] getHolidays(int year) {
        Set<Date> days = getHolidaySet(year);
        Date[] dates = days.toArray(new Date[days.size()]);
        Arrays.sort(dates);
        return dates;
    }

    /**
     * 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);
    }

    /**
     * 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();
    }

    /**
     * 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;
    }

    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();
    }
}