简体   繁体   中英

Why calendar.get(Calendar.MONTH) returns 10 instead of 12?

Why

calendarCurrent = Calendar.getInstance(Locale.US);
calendarCurrent.get(Calendar.MONTH)

returns 10 if it is 12 now?

月索引从0开始而不是从1开始,因此如果它已经返回10,则必须是11,在美国语言环境中可能还不是11

Months are zero based (an unfortunate design decision, IMHO) so 0 means January and 10 means November. It's December 1 here, but it might still be November 30 in some time zones.

I've just tested it and it returns 11, which is december as months are in [0, 11] .

From the javadoc :

month - the value used to set the MONTH calendar field. Month value is 0-based. eg, 0 for January.

Everything is fine. You probably tested it a little before midnight or in a non US timezone, no ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM