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:

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.

This entry was posted in J2EE. Bookmark the permalink.

4 Responses to Java static initializer question

  1. PaulH says:

    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?

  2. fadi zawaideh says:

    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

  3. pip says:

    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);

  4. Pip says:

    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 Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>