Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Java Tip 44: Calculating holidays and their observances

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

  • Print
  • Feedback

"Write once, run anywhere" is a big promise to keep when you are writing a Java applet to be used on the Internet. Most programmers may realize that the Internet goes further than the boundaries of countries, but their coding does not seem to reflect this in practice.

Many applets tend to assume that the user is in the same locale and country as the author of the code. What could be considered a straightforward applet may have a bigger impact on the user than was originally planned, especially when the user is using the applet in another locale, whose political borders -- but not the linguistic or cultural borders -- may have been erased by the Internet. Applets need to be cognizant of the environment in which they actually will run and need to determine on the fly what actions and behaviors to display.

Let's assume I wrote a Java calendar applet to be included on a Web page so that users could view and select certain dates for business purposes, for example, while avoiding a holiday. At first glance, this seems to be a simple project to complete. The initial question that pops into programmers' minds probably is "Which holidays do I indicate -- government/banking holidays and/or stock exchange holidays?" While this may seem to be a small wrinkle to iron out before coding, the problem is actually more involved than it appears. If we assume that all of the applet's users are from English-speaking countries that share the same holiday structure as the U.S., then any one of the multitude of calendar applets found in any random Java book would suffice. Unfortunately, this is real life, and the world is a little more complicated than a simple "How to..." book. So our little applet is going to dynamically calculate some of the holidays used in the U.S., Canada, and the rest of the world. By calculating the holidays, we can take the first step toward creating a truly dynamic runtime calendar applet that automatically adapts itself to the user's locale and displays the pertinent holidays for that user.

The facilities and idiosyncracies of Java

Before we start with the coding of the applet, let's take a look at some of the important facilities in Java that we will utilize. Java doesn't have the primitive date type that exists in many third- and fourth-generation languages. The "Date" object in Java is just that: an object used to encapsulate integer primitive types to store the year, month, date, and time values, along with some basic data manipulation methods. There is nothing exotic or glamorous there! The "Calendar" and "Gregorian Calendar" objects enhance the limited date manipulation facilities with a few more methods but overall are quite disappointing. The "Locale" object was released with the Java Development Kit (JDK) 1.1 and serves as a mechanism by which an applet determines the environment it must conform to as dictated by the norms of the user's country and language. Unfortunately, the Locale object currently seems to be more of a documentation myth: I have not been able to figure out how to extract the Locale information without first setting it. Numerous participants in the various Internet Java forums also have had no success getting the Locale object to work. It would be nice to see it auto-initialize and become an accurate source of information at runtime for applets. (Side note: If anyone knows how to successfully get the Locale object to perform, it would make a great follow up article to this one!)

  • Print
  • Feedback

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