Calendar.DAY_OF_WEEK获得的值不对的问题

      今天是2018年11月7号,学习了一下calendar类。下面的代码让我百思不得其解。

import java.util.Calendar;

import java.util.GregorianCalendar;

public class TestCalendar {

    public static void main(String[] args) {

        Calendar calendar=new GregorianCalendar(2018,11,7,15,58,32);//我以为是表示2018年11月7日15:58:32

        System.out.println(calendar.get(Calendar.DAY_OF_WEEK));//

    }

}


    今天是星期三,但是输出的值为6。想了许久,原来calendar的月份是从0开始的,所以11表示12月份,问题就出在了这里。这个calendar类的源码有定义:

/**

    * Value of the {@link #MONTH} field indicating the

    * first month of the year in the Gregorian and Julian calendars.

    */

    public static final int JANUARY = 0;

    /**

    * Value of the {@link #MONTH} field indicating the

    * second month of the year in the Gregorian and Julian calendars.

    */

    public static final int FEBRUARY = 1;

    /**

    * Value of the {@link #MONTH} field indicating the

    * third month of the year in the Gregorian and Julian calendars.

    */

    public static final int MARCH = 2;

    /**

    * Value of the {@link #MONTH} field indicating the

    * fourth month of the year in the Gregorian and Julian calendars.

    */

    public static final int APRIL = 3;

    /**

    * Value of the {@link #MONTH} field indicating the

    * fifth month of the year in the Gregorian and Julian calendars.

    */

    public static final int MAY = 4;

    /**

    * Value of the {@link #MONTH} field indicating the

    * sixth month of the year in the Gregorian and Julian calendars.

    */

    public static final int JUNE = 5;

    /**

    * Value of the {@link #MONTH} field indicating the

    * seventh month of the year in the Gregorian and Julian calendars.

    */

    public static final int JULY = 6;

    /**

    * Value of the {@link #MONTH} field indicating the

    * eighth month of the year in the Gregorian and Julian calendars.

    */

    public static final int AUGUST = 7;

    /**

    * Value of the {@link #MONTH} field indicating the

    * ninth month of the year in the Gregorian and Julian calendars.

    */

    public static final int SEPTEMBER = 8;

    /**

    * Value of the {@link #MONTH} field indicating the

    * tenth month of the year in the Gregorian and Julian calendars.

    */

    public static final int OCTOBER = 9;

    /**

    * Value of the {@link #MONTH} field indicating the

    * eleventh month of the year in the Gregorian and Julian calendars.

    */

    public static final int NOVEMBER = 10;

    /**

    * Value of the {@link #MONTH} field indicating the

    * twelfth month of the year in the Gregorian and Julian calendars.

    */

    public static final int DECEMBER = 11;

    /**

    * Value of the {@link #MONTH} field indicating the

    * thirteenth month of the year. Although GregorianCalendar

    * does not use this value, lunar calendars do.

    */

将代码改成如下:

import java.util.Calendar;

import java.util.GregorianCalendar;

public class TestCalendar {

    public static void main(String[] args) {

        Calendar calendar=new GregorianCalendar(2018,10,7,15,58,32);//表示2018年11月7日15:58:32

        System.out.println(calendar.get(Calendar.DAY_OF_WEEK));//

    }

}

输出结果为4,(我们想要3,但是第一天默认是星期天)。这个也是calender源码里面设置的:

/**

    * Value of the {@link #DAY_OF_WEEK} field indicating

    * Sunday.

    */

    public static final int SUNDAY = 1;

    /**

    * Value of the {@link #DAY_OF_WEEK} field indicating

    * Monday.

    */

    public static final int MONDAY = 2;

    /**

    * Value of the {@link #DAY_OF_WEEK} field indicating

    * Tuesday.

    */

    public static final int TUESDAY = 3;

    /**

    * Value of the {@link #DAY_OF_WEEK} field indicating

    * Wednesday.

    */

    public static final int WEDNESDAY = 4;

    /**

    * Value of the {@link #DAY_OF_WEEK} field indicating

    * Thursday.

    */

    public static final int THURSDAY = 5;

    /**

    * Value of the {@link #DAY_OF_WEEK} field indicating

    * Friday.

    */

    public static final int FRIDAY = 6;

    /**

    * Value of the {@link #DAY_OF_WEEK} field indicating

    * Saturday.

    */

    public static final int SATURDAY = 7;



你可能感兴趣的:(Calendar.DAY_OF_WEEK获得的值不对的问题)