Presents your JAVA E-NEWSLETTER for January 6, 2003 <-------------------------------------------> EXPLORE THE BENEFITS OF JAVA.UTIL.CALENDAR The Calendar class, introduced in JDK 1.1, is a different style of date handling class. Imagine it as a typical calendar on the wall, with lots of dates and pages that you can turn. The basis of the Calendar class is the concept of fields. Each time element is a field, and these fields are represented by static fields in the Calendar class. Using these fields, values may be obtained or set using the get/set methods. // a default Calendar, set to this instance in time Calendar cal = Calendar.getInstance(); int year = cal.get(Calendar.YEAR); cal.set(Calendar.MONTH, Calendar.NOVEMBER); The Calendar class provides the ability to move between dates with the add method and the roll method. Both methods take a field to modify and a value to modify it by, where that value may be positive or negative. The only difference is that add will overflow values into higher fields. For example, if you take away three days from September 3, you get: Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, -3); // value is: Sat Aug 31 23:43:19 EDT 2002 However rolling back three days will give: Calendar cal = Calendar.getInstance(); cal.roll(Calendar.DATE, -3); // value is: Mon Sep 30 23:43:47 EDT 2002 This is why you'll primarily use the add method. There is one more piece of functionality hidden in the most common subclass of the Calendar class--the isLeapYear method. Calendar cal = Calendar.getInstance(); boolean leapYear = ( (GregorianCalendar)cal ).isLeapYear(2002); // this is false Despite being an instance method, the isLeapYear method behaves like a static method, requiring the year in question to be passed into the calendar. By taking over the date-modification functionality, the java.util.Calendar class seems like a more complex version of Date. But the extra punch it carries, to say nothing of the internationalization abilities it has, makes it well worth the learning curve. ----------------------------------------