? ? ? 今天是2018年11月7號(hào)僵刮,學(xué)習(xí)了一下calendar類(lèi)缰泡。下面的代碼讓我百思不得其解末荐。
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狰域。想了許久邦蜜,原來(lái)calendar的月份是從0開(kāi)始的依鸥,所以11表示12月份,問(wèn)題就出在了這里悼沈。這個(gè)calendar類(lèi)的源碼有定義:
/**
? ? * 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 <code>GregorianCalendar</code>
? ? * 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));//
????}
}
輸出結(jié)果為4贱迟,(我們想要3,但是第一天默認(rèn)是星期天)絮供。這個(gè)也是calender源碼里面設(shè)置的:
/**
? ? * 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;