源码如下,当参数不是Calendar
的实例时返回的是false
,但请问这样的比较有意义吗?
我个人觉得参数类型是不是不妥?比如我拿Date
类型去做比较,不抱任何错,但其实永远返回的都是false
。
public boolean before(Object when) {
return when instanceof Calendar
&& compareTo((Calendar)when) < 0;
}
public boolean after(Object when) {
return when instanceof Calendar
&& compareTo((Calendar)when) > 0;
}
//我认为这样比较合理
public boolean after(Canlendar when) {
return compareTo(when) > 0;
}
巴扎黑2017-04-17 16:42:52
So in comparisonCalendar
直接用ComparaTo()
就好啦Date
和Calendar
it is generally accepted as bad.
Same question stackoverflow
I think there is no particular reason for that. java.util.Calendar has some design issues we have to live with, unfortunately.
Added:
Java8 implements the new package java.time, which is immutable and thread-safe. The main classes and formats in the package
correspond to the old API