简体   繁体   English

Java日历。 After函数未返回预期结果

[英]Java Calendar. After function is not returning expected result

I have written the below code 我写了下面的代码

Calendar now = Calendar.getInstance();
Calendar expiry = Calendar.getInstance();
expiry.set(2014, 1, 15, 0, 0); 
now.after(expiry);

this is giving me false, today is 19th it should give true 这给了我错误,今天是19日,应该给我真实

Am I missing anything? 我有什么想念的吗?

(1) Month 1 is February, not January as you thought. (1)第1个月是2月,而不是您想象的1月。 Month 0 is for January. 第0个月是一月。

(2) Also, I would call getTime() right before calling after() just to be on the safe side. (2)另外,为了安全起见,我会在调用after()之前立即调用getTime()。

Calendar now = Calendar.getInstance();
Calendar expiry = Calendar.getInstance();
expiry.set(2014, 0, 15, 0, 0); 
expiry.getTime();
now.after(expiry);

Not sure if calling getTime() here is strictly needed though. 但不确定是否严格需要在此处调用getTime()。

I am referring to this part of the JavaDoc. 我指的是JavaDoc的这一部分。

set(f, value) changes calendar field f to value. set(f,value)将日历字段f更改为value。 In addition, it sets an 此外,它还设置了
internal member variable to indicate that calendar field f has been changed. 内部成员变量,以指示日历字段f已更改。
Although calendar field f is changed immediately, the calendar's time value 尽管日历字段f立即更改,但日历的时间值
in milliseconds is not recomputed until the next call to get(), getTime(), 直到下一次调用get(),getTime(),
getTimeInMillis(), add(), or roll() is made. 制作了getTimeInMillis(),add()或roll()。 Thus, multiple calls to set() 因此,多次调用set()
do not trigger multiple, unnecessary computations. 不要触发多个不必要的计算。 As a result of changing 由于变化
a calendar field using set(), other calendar fields may also change, depending 使用set()的日历字段,其他日历字段也可能会更改,具体取决于
on the calendar field, the calendar field value, and the calendar system. 在日历字段,日历字段值和日历系统上。
In addition, get(f) will not necessarily return value set by the call to 此外,get(f)不一定会返回通过调用以下项设置的值
the set method after the calendar fields have been recomputed. 重新计算日历字段后的set方法。
The specifics are determined by the concrete calendar class. 具体情况由具体的日历类确定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM