Aaron Johnson Now with 50% less caffeine!

Posted
3 December 2004 @ 3pm

Tagged
J2EE

Java static initializer question

I ran into a problem with a section of code that lives within a Java static initializer; I’m not sure that the problem is the static init. Let me step back and frame the problem first. I have a Struts action class that presents a form for collecting date information; said form includes a drop-down menu that is bound to an ArrayList of LabelValueBean objects that contains the months of the year (Jan|1,Feb|2,Mar|3, etc..). The ArrayList of LabelValueBean objects is retrieved from a data provider class using a static method:
Collection months = MyDateProvider.getMonths();
Here’s the pseudo-code inside the date provider class:
public class MyDateProvider {
  private static Collection months = new ArrayList();
  
  static {
    Calendar c = Calendar.getInstance();
    SimpleDateFormat formatter = new SimpleDateFormat("MMM");
    SimpleDateFormat monthformatter = new SimpleDateFormat("MM");
    for (int i=0; i
The problem is that somehow the ArrayList of LabelValueBean objects contained two instances of March and no February, so the dropdown list would look like this:
January
March
March
April
May
June
July
August
September
October
November
December

Restarting Tomcat temporarily fixed the problem (obviously because MyDateProvider was then reloaded), but my question is how did is it possible for the ArrayList to be missing February and contain two instances of March? Any takers?

Update: the problem has nothing to do with the static init and everything to do with the way I used the Calendar class. The Calendar.getInstance() method returns a Calendar object initialized with the current date and time, so when the web application was restarted on 11/30/2004, the Calendar object reflected that date/time. Looping from 0 to 11 and setting the month (c.set(java.util.Calendar.MONTH, i);) resulted in the month changing correctly EXCEPT during the month of February , which has only 29 days. The date “02/30/2004″ is illegal and so the Calendar class helpfully bumps the month up, which resulted in two instances of March in my drop down. Thanks to Paul Hastings for pointing this out.


4 Comments

Posted by
PaulH
3 December 2004 @ 5pm

WAG but was the date when the calendar was init 28th/29th? as you’re only setting the month field you might be rolling teh calendar over into the next month (march). while i don’t use core java much (mostly use java for i18n work & icu4j is better for that), why not just grab the month strings using DateFormatSymbols?


Posted by
fadi zawaideh
28 December 2004 @ 3pm

please answer these questions
1)how to use static
2)when to use static
3)why to use static
4)where to use static
thanks alot


Posted by
pip
31 January 2005 @ 6am

someone tells me:
for(int i=0;i<12;i++){
//add this first to make sure it’s not 30 or 31
c.set(Calendar.DATE, 1);
//…
}


Posted by
Pip
31 January 2005 @ 6am

Other told me:
for(int i=0;i<12;i++){
//add this first so it won’t be 30 or 31
c.set(Calendar.DATE, 1);
//……
}


Leave a Comment