javacalendar

Calendar class java


I am setting the value to be 23 January 2009, but when I ask for month it returns '2' Here is my code:

Calendar calendar=GregorianCalendar.getInstance();  
calendar.set(2009,01,23);  
calendar.getTime();  
System.out.println(calendar.MONTH);  

Please help! I expect the first output to be 1, not 2!


Solution

  • Calendar.MONTH is a constant. It's used to indicate that you want the month field, using calendar.get():

    System.out.println(calendar.get(Calendar.MONTH));