在java中使用Calendar类

时间:2014-09-01 06:09:50

标签: java

我在Calendar类上做了一个简单的测试。代码是:

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));

    }

得到的结果是:

Nepal Time
Nepal Time
0
9
9

为什么我将MONTH作为' 9'对于cal1而不是' 8' ??

5 个答案:

答案 0 :(得分:3)

由于月份的编号从0开始,因此8个月为September,其中30天为31天。

但是你将这一天设置为1。额外的October天会添加到下个月9,月号cal1。因此,cal2October都有月份 - 9

这就是为什么它在两种情况下都显示{{1}}的原因。

答案 1 :(得分:2)

在日历中,月份数组从零开始,因此1月份为0.在8月31日,系统认为你说的是​​9月31日,这将是第9个月,所以在阵列中排名第8。 9月31日不存在,因此系统在月份到10月份出现颠簸,因此排在第9位。

我建议使用日历常量而不是数月的数字。

Calendar.AUGUST
Calendar.SEPTEMBER

答案 2 :(得分:2)

如果您查看java.util.Calendar类的源代码(位于JDK文件夹中的src.zip中),您将看到以下代码:

 /**
     * 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;

答案 3 :(得分:1)

  

为什么我将MONTH作为' 9'对于cal1而不是' 8' ??

因为月份从0开始。

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

我建议您使用日历常量字段数月,例如Calendar.JANUARYCalendar.FEBRUARY ... Calendar.DECEMBER以避免混淆。

答案 4 :(得分:0)

这是因为cal1.set(2014, 8, 31);在9月31日(基于零)实例化日历。因为9月只有30天,它将成为10月的第一天,这也就是为什么你得到9作为基于零的索引的10月份的原因。