1

我正在尝试使用以下代码段将日期设置为 2012 年 12 月 31 日,但我得到的是 2013 年 3 月 3 日,这里有什么问题。

Calendar today = Calendar.getInstance();
today.set(Calendar.MONTH, 13);
today.set(Calendar.DAY_OF_MONTH, 31);
today.set(Calendar.YEAR, 2012);

long calendarNeverEndDate = today.getTime().getTime();
System.out.println("calendarNeverEndDate:"
                   + sdf.formatLocal(calendarNeverEndDate));
4

1 回答 1

5

That's because months are 0-indexed (yes, it's inconsistent with days :-( ).

edit: As Romain mentioned, it's better to use constants

Calendar.DECEMBER
于 2012-05-07T14:19:49.867 回答