search

Home  >  Q&A  >  body text

java - 为什么Calendar类的before,after方法的参数是Object类型的?

源码如下,当参数不是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中文网2805 days ago839

reply all(1)I'll reply

  • 巴扎黑

    巴扎黑2017-04-17 16:42:52

    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

    reply
    0
  • Cancelreply