简体   繁体   中英

Wrong default fields values in java.util.calendar

I'm creating an instance of java.util.Calendar class:

Calendar rightNow = Calendar.getInstance();

getInstance() gets a Calendar using the default time zone and locale... Its 20:22 1.11.2012 in Poland right now, but when I print rightNow field out, the result is wrong:

System.out.print(rightNow.get(Calendar.MONTH));
result: 10.

System.out.print(rightNow.get(Calendar.DAY_OF_MONTH));
result: 1.

System.out.print(rightNow.get(Calendar.DATE));
result: 1.

I supose that in December the YEAR field will also be wrong. Can somebody explain that to me?

Month starts from 0 when you use calendar not from 1

As per javadoc

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.

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