1

I did a simple test on Calendar class. The code is :

public static void main(String[] args) {

        TimeZone tz = TimeZone.getTimeZone("Asia/Kathmandu");
        System.out.println(tz.getDisplayName());

        Calendar cal1 = Calendar.getInstance();
        cal1.set(2014, 8, 31);

        System.out.println(cal1.getTimeZone().getDisplayName());
        Calendar cal2 = Calendar.getInstance();
        cal2.set(2014,9,1);

        int diff = cal2.get(Calendar.MONTH)-cal1.get(Calendar.MONTH);
        System.out.println(diff);
        System.out.println(cal2.get(Calendar.MONTH));
        System.out.println(cal1.get(Calendar.MONTH));

    }

The result got was :

Nepal Time
Nepal Time
0
9
9

Why am I getting MONTH as '9' for cal1 instead of '8'??

SudeepShakya
  • 571
  • 3
  • 14
  • 34
  • 1
    Consider using the new `java.time` API if you can. There's much fewer surprises there. – jdphenix Sep 01 '14 at 06:13
  • 1
    possible duplicate of [Why is January month 0 in Java Calendar?](http://stackoverflow.com/questions/344380/why-is-january-month-0-in-java-calendar) – jdphenix Sep 01 '14 at 06:13
  • 1
    `cal1.set(2014, 8, 31);` should be `cal1.set(2014, 7, 31);` because january is `0`. Now you get an overflow to the next month because there are not 31 days in September. – john Sep 01 '14 at 06:18

5 Answers5

3

As the numbering of month starts at 0, so the 8th month is September, which has 30 days.

But you set the day to 31. The extra 1 day is added to the next month October, month number 9. So, both cal1 and cal2 has the month - October.

That's why it shows 9 in both cases.

Kawsar Ahmed
  • 433
  • 2
  • 12
2

In Calendar, the array of months starts at zero, so January is 0. Putting in 8/31, the system thinks you are saying September 31st, which would be the 9th month, so 8th in the array. September 31st doesn't exist, so the system bumps the month up to October, so 9th in the array.

I would suggest using the Calendar constants instead of numbers for the months.

Calendar.AUGUST
Calendar.SEPTEMBER
WoogieNoogie
  • 1,258
  • 1
  • 11
  • 21
2

If you look inside of the source code (located in src.zip in your JDK folder) of java.util.Calendar class you will see the following code:

 /**
     * Value of the {@link #MONTH} field indicating the
     * first month of the year in the Gregorian and Julian calendars.
     */
    public final static int JANUARY = 0;

    /**
     * Value of the {@link #MONTH} field indicating the
     * second month of the year in the Gregorian and Julian calendars.
     */
    public final static int FEBRUARY = 1;

    /**
     * Value of the {@link #MONTH} field indicating the
     * third month of the year in the Gregorian and Julian calendars.
     */
    public final static int MARCH = 2;

    /**
     * Value of the {@link #MONTH} field indicating the
     * fourth month of the year in the Gregorian and Julian calendars.
     */
    public final static int APRIL = 3;

    /**
     * Value of the {@link #MONTH} field indicating the
     * fifth month of the year in the Gregorian and Julian calendars.
     */
    public final static int MAY = 4;

    /**
     * Value of the {@link #MONTH} field indicating the
     * sixth month of the year in the Gregorian and Julian calendars.
     */
    public final static int JUNE = 5;

    /**
     * Value of the {@link #MONTH} field indicating the
     * seventh month of the year in the Gregorian and Julian calendars.
     */
    public final static int JULY = 6;

    /**
     * Value of the {@link #MONTH} field indicating the
     * eighth month of the year in the Gregorian and Julian calendars.
     */
    public final static int AUGUST = 7;

    /**
     * Value of the {@link #MONTH} field indicating the
     * ninth month of the year in the Gregorian and Julian calendars.
     */
    public final static int SEPTEMBER = 8;

    /**
     * Value of the {@link #MONTH} field indicating the
     * tenth month of the year in the Gregorian and Julian calendars.
     */
    public final static int OCTOBER = 9;

    /**
     * Value of the {@link #MONTH} field indicating the
     * eleventh month of the year in the Gregorian and Julian calendars.
     */
    public final static int NOVEMBER = 10;

    /**
     * Value of the {@link #MONTH} field indicating the
     * twelfth month of the year in the Gregorian and Julian calendars.
     */
    public final static int DECEMBER = 11;
1

Why am I getting MONTH as '9' for cal1 instead of '8'??

Because months starts with 0.

0 for January
1 for February
...
11 for December

I recommend you use the Calendar constant fields for months like Calendar.JANUARY, Calendar.FEBRUARY ... Calendar.DECEMBER to avoid confusion.

lxcky
  • 1,668
  • 2
  • 13
  • 26
  • 2
    Even if months start with 0, I set the value to '8' explicitly.And has displayed '9' for the month set to '9'.Why Not '10' ? – SudeepShakya Sep 01 '14 at 06:20
  • Instead of throwing an exception for an illegal date (31st of September - 08-31 zero-indexed) it 'corrects' this and makes it the 1st of October (09-01). So both instances are set to the same day. So the actual value of 08-31 is changed to 09-01, it's not just a display thing. – Davio Sep 01 '14 at 06:25
0

That is because the cal1.set(2014, 8, 31); instanciate a calendar with the day 31 of september (zero based). because the september has only 30 days it will be the first of october and thats why you get 9 as month what is the october in a zero based index.

Jens
  • 67,715
  • 15
  • 98
  • 113