Search code examples
javaandroiddategregorian-calendar

Gregorian Calendar - Getting wrong values in Android


I am using Gregorian calendar to set the date value and trying to get the values set. Code below

 Constants.sSetDate_calendar = new GregorianCalendar(year,month,day);// year - 2013, month - 12, day - 15
          Log.w("OOOOO", Constants.sSetDate_calendar.get(Calendar.YEAR)+"--"+Constants.sSetDate_calendar.get(Calendar.MONTH)+"--"+Constants.sSetDate_calendar.get(Calendar.DAY_OF_MONTH)+"");   

In Log statement, i see

11-02 10:21:35.060: W/OOOOO(26671): **2014--0--15**

Not sure what is going wrong. Why the values set and get are different ? Need some help here.


Solution

  • The Calendar class uses a zero-based array meaning January is 0 and December is 11 as can be seen by looking at the Calendar.JANUARY and Calendar.DECEMBER values. The 12th month simply overflows into the next year (December + 1 month) which is why the values change.