Java GregorianCalendar Fields

I try to convert a String to GregorianCalendar, and then check it. Below, I post my codes. I really confuse that why the fields seem wrong. Thanks a lot for help.

public class test{

    private static final ArrayList<String> MONTH_OF_YEAR = new ArrayList(Arrays.asList( "Jan", "Feb", "Mar", "Apr", "May", 
          "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"));

    static GregorianCalendar date;

    public static void main(String[] args){
        String t = "28-Mar-2099 11294.94 11279.65";
        String[] splitStr = t.split(" ");

        System.out.println(splitStr[0]);

        date = ConvertToDate(splitStr[0]);
        System.out.println(date.getTime());

        System.out.println(date.DAY_OF_MONTH);
        System.out.println(date.MONTH);
        System.out.println(date.YEAR);

    }


    private static GregorianCalendar ConvertToDate(String dt){
        String[] splitStr = dt.split("-");
        if(splitStr.length != 3){
            throw new IllegalArgumentException();
        }
        int dayOfMonth = Integer.parseInt(splitStr[0]);
        int year = Integer.parseInt(splitStr[2]);
        int month = MONTH_OF_YEAR.indexOf(splitStr[1]);
        if(month < 0 || month > 11){
            throw new IllegalArgumentException();
        }
        return new GregorianCalendar(year, month, dayOfMonth);
    }
}

And the outputs are like these:

28-Mar-2099
Sat Mar 28 00:00:00 PDT 2099
5
2
1

Could anyone help me explain why that the fields don't match the date?

Jon Skeet
people
quotationmark

These:

System.out.println(date.DAY_OF_MONTH);
System.out.println(date.MONTH);
System.out.println(date.YEAR);

Should be:

System.out.println(date.get(Calendar.DAY_OF_MONTH));
System.out.println(date.get(Calendar.MONTH));
System.out.println(date.get(Calendar.YEAR));

(And then remember that Calendar.MONTH is 0-based.)

The DAY_OF_MONTH etc values are constants, basically indicating which field you want to fetch. The calendar API is fundamentally pretty awful, unfortunately :( If you can possibly use Joda Time or the Java 8 java.time API instead, they're both much better.

Also note that there's really no need to write your own calendar parsing code - it already exists with SimpleDateFormat etc.

people

See more on this question at Stackoverflow