简体   繁体   中英

Use of Calendar class in java

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'??

As the numbering of month starts at 0 , so the 8 th 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.

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

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;

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM