July 25, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Class of the Month: GregorianCalendar

  • April 3, 2001
  • By Piroz Mohseni
  • Send Email »
  • More Articles »

Business applications often require date and time calculations. Furthermore, in a global environment, these calculations must be localized for various geographical regions in which the application is running. The Java platform took a huge step in streamlining the way it deals with date and time by introducing the abstract java.util.Calendar class. Subclasses of Calendar interpret a date according to the rules of a specific calendar system. One such subclass is java.util.GregorianCalendar. You can use the methods and properties of GregorianCalendar for many of your date and time calculations and comparisons.

Piroz Mohseni

You have a few choices with the constructor. If you are happy with the default time zone and default locale object, then you can use the null constructor (no arguments) or one of the constructors that allows you to specify the year, month, date, hour, and minutes. For applications that could potentially be used in different regions, you probably want to be more specific about the Locale and TimeZone objects. There are constructors that allow you to specify a Locale, a TimeZone, or both to create the GregorianCalendar object. If you are going to use TimeZone, take a look at java.util.SimpleTimeZone, which is a concrete subclass of TimeZone.

" The GreogorianCalendar class offers most of the information and calculations a typical date-based application needs."

Most of the useful methods associated with GregorianCalendar are inherited from its parent class Calendar. A common method for retrieving information is the get() method. This method takes a field associated with the Calendar class as its argument and returns its value. Examples include YEAR, MONTH, DAY_OF_WEEK, DAY_OF_YEAR, HOUR, MINUTE, and SECOND. There is also a set() method, which allows you to manually set many of the fields. If you specify values that are out of range (e.g., 45 for MONTH), then the numbers will be rolled into the next higher field, which for MONTH is YEAR. This can be confusing if you are not careful.

Comparing dates always seems to be a challenge, because many factors must be taken into account. The two methods after() and before() simplify the comparison. They each take a calendar object and return a boolean indicating whether chronologically the given calendar object falls after or before the current instance of calendar. The following code shows this:

import java.util.*;

public class caltest {

    public static void main(String args[]) {
        Calendar calendar = new GregorianCalendar();
        Calendar calendar2 = new GregorianCalendar();

        calendar2.set(Calendar.DAY_OF_MONTH, 8);

        System.out.println("Cal1 YEAR: " + calendar.get(Calendar.YEAR));
        System.out.println("Cal1 MONTH: " + calendar.get(Calendar.MONTH));
        System.out.println("Cal1 DAY: " + calendar.get(Calendar.DAY_OF_MONTH));
       
        System.out.println("Cal2 YEAR: " + calendar2.get(Calendar.YEAR));
        System.out.println("Cal2 MONTH: " + calendar2.get(Calendar.MONTH));
        System.out.println("Cal2 DAY: " + calendar2.get(Calendar.DAY_OF_MONTH));
       
        if (calendar.before(calendar2)) {
             System.out.println("Cal1 is before Cal2");
       }
       else {
             System.out.println("Cal1 is after Cal2");
       }
    }
}

For cases where you need to add a "fixed" time (invoice due dates, deadlines, etc.) you can use the add() method, which takes a field from the Calendar class and an integer representing the delta value to be added. To do a subtraction, the delta value will be negative. For example, to add 4 hours to the current time, use:

   cal.add(Calendar.HOUR, 4).

The GregorianCalendar class offers most of the information and calculations a typical date-based application needs. By joining hands with the Locale and TimeZone classes, it offers applications a consistent framework for localizing time and date information, thus offering a broad deployment base.

About the Author

Piroz Mohseni is president of Bita Technologies, focusing on business improvement through the effective use of technology. His areas of interest include enterprise Java, XML, and e-commerce applications.






Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel