Tips on Java Calendar

Tips on Java Calendar

 

The recent project has got me a great chance to work quite comprehensively to the Java Calendar related stuff. Few things have been found worth of a note.

 

First of all, you have to always remember that java.util.Calendar is an abstract class and you can only get an instance by calling the getInstance() method, just like other singleton class. This is very important to notice especially when you need to create a list of the customised dates. For example, your SQL ResultSet returns a set of dates and you need to hold them in an ArrayList. If you use the following code, you will only hold one date (the last record) in your array list as when you loop through the result set the value of the calendar instance will update by the latest record.

 

ArrayList<Calendar> latestDays = new ArrayList<Calendar>();

           

while (resultSet.next()) {               

Date tempDate = resultSet.getDate("user_timestamp");

Calendar calendar = Calendar.getInstance(); 

    calendar.setTime(tempDate);

    latestDays.add(tempCal);     

}   

 

The solution is to using the Calendar class in combination GregorianCalendar class. GregorianCalendar is extended from Calendar class and can be instantiated.

 

ArrayList<Calendar> latestDays = new ArrayList<GregorianCalendar>();

Calendar calendar = Calendar.getInstance(); 

           

while (resultSet.next()) {

    Date tempDate = resultSet.getDate("user_timestamp");

    calendar.setTime(tempDate);

    GregorianCalendar tempCal = new GregorianCalendar(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));

    tempCal.setFirstDayOfWeek(Calendar.MONDAY);

    latestDays.add(tempCal);               

}

 

Second, please be aware when you initialise a GregorianCalendar date like in the preceding example, you only assign the date info to your instance and you can’t expect it would automatically assign other values, such as WEEK_OF_MONTH, DAY_OF_WEEK and etc.

 

Another thing that I really want to address is the FirstDayOfWeek issues. I discovered the problem as the calendar I drew looked different if they were deployed on different machine. The default FirstDayOfWeek on my machine is Monday and the default FirstDayOfWeek on our product server is Sunday. Therefore, for example, 02/11/2008 is recognised as a day of the first WEEK_OF_MONTH on my machine, but is recognised as a day of the second WEEK_OF_MONTH on the product server. To make the calendar display identically, you have to manually set FirstDayOfWeek like the code above (as highlighted in yellow).

 

 

 

Reference

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Calendar.html

http://java.sun.com/j2se/1.5.0/docs/api/java/util/GregorianCalendar.html

 

Advertisements
This entry was posted in Java Utilities. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s