本文介紹了為什么 Java Calendar set(int year, int month, int date) 沒有返回正確的日期?的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
限時送ChatGPT賬號..
根據(jù)文檔,日歷 set() 是:
According to doc, calendar set() is:
http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Calendar.html#set%28int,%20int,%20int%29
set(int year, int month, int date)
Sets the values for the calendar fields YEAR, MONTH, and DAY_OF_MONTH.
代碼:
Calendar c1 = GregorianCalendar.getInstance();
c1.set(2000, 1, 30); //January 30th 2000
Date sDate = c1.getTime();
System.out.println(sDate);
輸出:
Wed Mar 01 19:32:21 JST 2000
為什么不是 1 月 30 日???
推薦答案
1 月是二月.2 月 30 日改為 3 月 1 日.您應(yīng)該為月份設(shè)置 0.最好是使用 Calendar 中定義的常量:
1 for month is February. The 30th of February is changed to 1st of March. You should set 0 for month. The best is to use the constant defined in Calendar:
c1.set(2000, Calendar.JANUARY, 30);
這篇關(guān)于為什么 Java Calendar set(int year, int month, int date) 沒有返回正確的日期?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!