本文介紹了為什么我在用 java 日歷解析時(shí)會(huì)得到錯(cuò)誤的月份的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
限時(shí)送ChatGPT賬號(hào)..
Date fakeDate = sdf.parse("15/07/2013 11:00 AM");
Calendar calendar = Calendar.getInstance()
calendar.setTime(fakeDate);
int currentMonth = calendar.get(Calendar.MONTH);
我得到 currentMonth == 6 而不是 7.
I get currentMonth == 6 instead of 7.
這是為什么呢?
推薦答案
因?yàn)?Calendar.MONTH
是從零開(kāi)始的.為什么?
Because Calendar.MONTH
is ZERO based. Why?
查看文檔:(總是)
get 和 set 的字段編號(hào),表示月份.這是一個(gè)特定于日歷的值.公歷一年的第一個(gè)月儒略歷是 JANUARY,即 0;最后取決于一年中的月數(shù).
Field number for get and set indicating the month. This is a calendar-specific value. The first month of the year in the Gregorian and Julian calendars is JANUARY which is 0; the last depends on the number of months in a year.
這篇關(guān)于為什么我在用 java 日歷解析時(shí)會(huì)得到錯(cuò)誤的月份的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!