Calendar.DAY_OF_WEEK獲得的值不對(duì)的問(wèn)題

? ? ? 今天是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;



?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末衣吠,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子壤靶,更是在濱河造成了極大的恐慌缚俏,老刑警劉巖,帶你破解...
    沈念sama閱讀 212,816評(píng)論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件贮乳,死亡現(xiàn)場(chǎng)離奇詭異忧换,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)塘揣,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,729評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門(mén)包雀,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人亲铡,你說(shuō)我怎么就攤上這事才写。” “怎么了奖蔓?”我有些...
    開(kāi)封第一講書(shū)人閱讀 158,300評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵赞草,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我吆鹤,道長(zhǎng)厨疙,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 56,780評(píng)論 1 285
  • 正文 為了忘掉前任疑务,我火速辦了婚禮沾凄,結(jié)果婚禮上梗醇,老公的妹妹穿的比我還像新娘。我一直安慰自己撒蟀,他們只是感情好叙谨,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,890評(píng)論 6 385
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著保屯,像睡著了一般手负。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上姑尺,一...
    開(kāi)封第一講書(shū)人閱讀 50,084評(píng)論 1 291
  • 那天竟终,我揣著相機(jī)與錄音,去河邊找鬼切蟋。 笑死统捶,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的敦姻。 我是一名探鬼主播瘾境,決...
    沈念sama閱讀 39,151評(píng)論 3 410
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼镰惦!你這毒婦竟也來(lái)了迷守?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 37,912評(píng)論 0 268
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤旺入,失蹤者是張志新(化名)和其女友劉穎兑凿,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體茵瘾,經(jīng)...
    沈念sama閱讀 44,355評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡礼华,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,666評(píng)論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了拗秘。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片圣絮。...
    茶點(diǎn)故事閱讀 38,809評(píng)論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖雕旨,靈堂內(nèi)的尸體忽然破棺而出扮匠,到底是詐尸還是另有隱情,我是刑警寧澤凡涩,帶...
    沈念sama閱讀 34,504評(píng)論 4 334
  • 正文 年R本政府宣布棒搜,位于F島的核電站,受9級(jí)特大地震影響活箕,放射性物質(zhì)發(fā)生泄漏力麸。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,150評(píng)論 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望克蚂。 院中可真熱鬧闺鲸,春花似錦、人聲如沸陨舱。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,882評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)游盲。三九已至,卻和暖如春蛮粮,著一層夾襖步出監(jiān)牢的瞬間益缎,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,121評(píng)論 1 267
  • 我被黑心中介騙來(lái)泰國(guó)打工然想, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留莺奔,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 46,628評(píng)論 2 362
  • 正文 我出身青樓变泄,卻偏偏與公主長(zhǎng)得像令哟,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子妨蛹,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,724評(píng)論 2 351

推薦閱讀更多精彩內(nèi)容