Calendar.before(Object when), why Object?
From the javadoc of Calendar.before(Object when)
:
Returns whether this Calendar represents a time before the time represented by the specified Object. This method is equivalent to:
compareTo(when) < 0
if and only if when is a Calendar instance. Otherwise, the method returns false.
Why does it accepts an Object if when someone passes something that's n开发者_如何学Cot a Calendar instance it returns false? Why not just accepting a Calendar instance? This kept me watching for uncorrect results in a functionality for quite some time.
I think there is no particular reason for that. java.util.Calendar
has some design issues we have have to live with, unfortunately.
Note that the method is not final
(and the class itself abstract). Feel free to subclass it and provide a version which will take Java’s Date
or JodaTime’s LocalDate
. If you see it that way, it turns out that it was very far-sighted of the JDK’s authors to provide such a general solution.
I think this may be to provide encapsulation by using polymorphic behaviour in the before() method.
精彩评论