Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Java Tip 44: Calculating holidays and their observances

Figure out all of those cool days off that you get for holidays

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Page 3 of 6

The Islamic and Gregorian calendars

The Islamic calendar (or "Hijri" calendar) is purely a lunar calendar. It contains 12 months that are based on the motion of the moon, and because 12 synodic months are only 29.53 days, and 12 * 29.53 = 354.36 days, the Islamic calendar is consistently shorter than a 365-day tropical year. Therefore, the Islamic calendar shifts with respect to the Gregorian calendar. The Islamic calendar is the official calendar in countries around the Gulf, especially Saudi Arabia. But other Muslim countries use the Gregorian calendar for civil purposes and only turn to the Islamic calendar for religious purposes.

For added complexity, the months in the Islamic calendar only change when the lunar crescent is first seen (by an actual human being) after a new moon. Although new moons may be calculated quite precisely, the actual visibility of the crescent is much more difficult to predict. It depends on factors such as weather, the atmosphere, and the location of the observer. It is therefore very difficult to give accurate information in advance about when a new month will start. Some Muslims depend on a local sighting of the moon, whereas others depend on a sighting by authorities somewhere in the Muslim world. Both are valid Islamic practices, but they may lead to different starting days for the months.

Coding for the U.S. holidays

Let's take a look at some of the U.S. holidays -- when they are observed and how we can calculate them using Java. Here are some of the standard United States holidays that are calculated according to the following rules: New Year's Day observance seems fairly simple at first until we remember the wrinkle in the calculation that if the holiday falls on a Saturday, then often the day off is the preceding Friday, December 31st -- New Year's Eve.

    public static Date NewYearsDay (int nYear)
    {
    // January 1st
    int nMonth = 0; // January
    return new Date(nYear, nMonth, 1);
    }
    public static Date NewYearsDayObserved (int nYear)
    {
    int nX;
    int nMonth = 0;         // January
    int nMonthDecember = 11;    // December
    Date dtD;
    dtD = new Date(nYear, nMonth, 1);
    nX = dtD.getDay();
    if (nYear > 1900)
        {
        nYear -= 1900;
        }
    switch(nX)
        {
        case 0 : // Sunday
        return new Date(nYear, nMonth, 2);
        case 1 : // Monday
        case 2 : // Tuesday
        case 3 : // Wednesday
        case 4 : // Thursday
        case 5 : // Friday
        return new Date(nYear, nMonth, 1);
        default :
        // Saturday, then observe on friday of previous year
        return new Date(--nYear, nMonthDecember, 31);
        }
    }


Martin Luther King Day is observed on the third Monday in January. The most complicated aspect of this holiday is determining which states and municipalities observe it! Certain states do not recognize this day formally as a holiday and conduct business as usual.

    public Date MartinLutherKingObserved (int nYear)
    {
    // Third Monday in January
    int nX;
    int nMonth = 0; // January
    Date dtD;
    dtD = new Date(nYear, nMonth, 1);
    nX = dtD.getDay();
    switch(nX)
        {
        case 0 : // Sunday
        return new Date(nYear, nMonth, 16);
        case 1 : // Monday
        return new Date(nYear, nMonth, 15);
        case 2 : // Tuesday
        return new Date(nYear, nMonth, 21);
        case 3 : // Wednesday
        return new Date(nYear, nMonth, 20);
        case 4 : // Thursday
        return new Date(nYear, nMonth, 19);
        case 5 : // Friday
        return new Date(nYear, nMonth, 18);
        default : // Saturday
        return new Date(nYear, nMonth, 17);
        }
    }


President's Day, formerly Washington and Lincoln's birthday, is celebrated on the third Monday in February.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (1)
Login
Forgot your account info?

Open source projectBy Anonymous on October 31, 2009, 12:24 amhttp://code.google.com/p/jholidays/

Reply | Read entire comment

View all comments

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources
  • Calendar Applet http://www.javaworld.com/javatips/javatip44/CorporateCalendar.java
  • Holiday Calculation Library http://www.javaworld.com/javatips/javatip44/Holidays.java
  • Global Interface Design, by Tony Fernandes, ISBN 0-12-253790-4.
  • The Calendar FAQ, by Claus Tondering
    http://www.tondering.dk/claus/calendar.html