本文介紹了為什么我在用 java 日歷解析時會得到錯誤的月份的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
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.
這是為什么呢?
推薦答案
因為 Calendar.MONTH
是從零開始的.為什么?
Because Calendar.MONTH
is ZERO based. Why?
查看文檔:(總是)
get 和 set 的字段編號,表示月份.這是一個特定于日歷的值.公歷一年的第一個月儒略歷是 JANUARY,即 0;最后取決于一年中的月數.
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.
這篇關于為什么我在用 java 日歷解析時會得到錯誤的月份的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!