Календарь возвращает неправильный месяц

Я нашел этот ссылка , который отвечает на него вполне хорошо. Это использует:

(ENUMTYPE)Enum.ToObject(typeof(ENUMTYPE), INT)

98
задан Hant 18 November 2009 в 10:53
поделиться

6 ответов

Месяцы индексируются с 0, а не с 1, поэтому 10 - ноябрь, а 11 - декабрь.

130
ответ дан 24 November 2019 в 05:12
поделиться

Они начинаются с 0 - проверьте документы

28
ответ дан 24 November 2019 в 05:12
поделиться

Как видно из многих ответов: месяц начинается с 0.

Вот совет: вам следует использовать SimpleDateFormat для получения строкового представления месяца:

Calendar rightNow = Calendar.getInstance();
java.text.SimpleDateFormat df1 = new java.text.SimpleDateFormat("MM");
java.text.SimpleDateFormat df2 = new java.text.SimpleDateFormat("MMM");
java.text.SimpleDateFormat df3 = new java.text.SimpleDateFormat("MMMM");
System.out.println(df1.format(rightNow.getTime()));
System.out.println(df2.format(rightNow.getTime()));
System.out.println(df3.format(rightNow.getTime()));

Вывод:

11
Nov
November

Примечание: вывод может отличаться, он зависит от локали.

22
ответ дан 24 November 2019 в 05:12
поделиться

As several people have pointed out, months returned by the Calendar and Date classes in Java are indexed from 0 instead of 1. So 0 is January, and the current month, November, is 10.

You might wonder why this is the case. The origins lie with the POSIX standard functions ctime, gmtime and localtime, which accept or return a time_t structure with the following fields (from man 3 ctime):

int tm_mday;    /* day of month (1 - 31) */
int tm_mon;     /* month of year (0 - 11) */
int tm_year;    /* year - 1900 */

This API was copied pretty much exactly into the Java Date class in Java 1.0, and from there mostly intact into the Calendar class in Java 1.1. Sun fixed the most glaring problem when they introduced Calendar – the fact that the year 2001 in the Gregorian calendar was represented by the value 101 in their Date class. But I'm not sure why they didn't change the day and month values to at least both be consistent in their indexing, either from zero or one. This inconsistency and related confusion still exists in Java (and C) to this day.

18
ответ дан 24 November 2019 в 05:12
поделиться

Месяцы начинаются с нуля , например индексы для списков.

Следовательно, январь = 0, февраль = 1 и т. д.

11
ответ дан 24 November 2019 в 05:12
поделиться

Из API:

Первый месяц года - ЯНВАРЬ. который равен 0; последнее зависит от количество месяцев в году.

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Calendar.html

9
ответ дан 24 November 2019 в 05:12
поделиться
Другие вопросы по тегам:

Похожие вопросы: