为什么 Java Calendar set(int year, int month, int date) 没有返回正确的日期?

2022-01-11 00:00:00 calendar java

根据文档,日历 set() 是:

According to doc, calendar set() is:

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Calendar.html#set%28int,%20int,%20int%29

set(int year, int month, int date) 
Sets the values for the calendar fields YEAR, MONTH, and DAY_OF_MONTH.

代码:

Calendar c1 = GregorianCalendar.getInstance();
c1.set(2000, 1, 30);  //January 30th 2000
Date sDate = c1.getTime();

System.out.println(sDate);

输出:

Wed Mar 01 19:32:21 JST 2000

为什么不是 1 月 30 日???

推荐答案

1 月是二月.2 月 30 日改为 3 月 1 日.您应该为月份设置 0.最好是使用 Calendar 中定义的常量:

1 for month is February. The 30th of February is changed to 1st of March. You should set 0 for month. The best is to use the constant defined in Calendar:

c1.set(2000, Calendar.JANUARY, 30);

相关文章