java - 为什么Calendar类的before,after方法的参数是Object类型的?
PHP中文网
PHP中文网 2017-04-17 16:40:52
0
1
842

源码如下,当参数不是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;
}
PHP中文网
PHP中文网

认证高级PHP讲师

reply all(1)
巴扎黑

So in comparisonCalendar直接用ComparaTo()就好啦

DateCalendar 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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template