为什么 Java Calendar set(intyear,intmonth,intdate) 不返回正确的日期?
根据文档,日历 set() 是:
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 日???
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.
code:
Calendar c1 = GregorianCalendar.getInstance();
c1.set(2000, 1, 30); //January 30th 2000
Date sDate = c1.getTime();
System.out.println(sDate);
output:
Wed Mar 01 19:32:21 JST 2000
Why it's not Jan 30 ???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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:
Calendar 对象中的月份从 0 开始
Months in Calendar object start from 0
示例中选择的日期很有趣。示例代码块为:
并输出
Wed Mar 01 19:32:21 JST 2000
。当我第一次阅读这个例子时,我认为输出是错误的,但它是正确的:)
Calendar.Month
从 0 开始,所以 1 表示二月。Selected date at the example is interesting. Example code block is:
and output
Wed Mar 01 19:32:21 JST 2000
.When I first read the example i think that output is wrong but it is true:)
Calendar.Month
is starting from 0 so 1 means February.