| Author |
Why do Calendar.after(...) and before(...) take an Object?
|
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12907
|
|
Class java.util.Calendar has two methods that you can use to see if a calendar is currently set to a date and time after or before that of another Calendar: public boolean after(Object when) public boolean before(Object when) For some strange reason, these methods take an Object as a parameter. According to the Javadoc, that object must be a Calendar. So why is the parameter type Object instead of Calendar? Is this just a major crazy design bug in the API or is there a reason for this?
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Paul Sturrock
Bartender
Joined: Apr 14, 2004
Posts: 10336
|
|
Java and anything to do with dates just seems to be badly done. I can't think of a good reason. But then maybe I'm just bitter after spending an afternoon fixing jdk1.5 introduced ClassCastExceptions in TimeStamp's compareTo method.
|
JavaRanch FAQ HowToAskQuestionsOnJavaRanch
|
 |
John Kelty
Ranch Hand
Joined: Aug 08, 2006
Posts: 33
|
|
I suppose it could be so that developers could write subclasses of Calendar that work with various data types, maybe Date, or maybe some proprietary class, without breaking the Calendar.before(Object when) method signature. That doesn�t make it right, but that�s my guess as to why they did it that way.
|
 |
Chris Rudd
Greenhorn
Joined: Nov 22, 2006
Posts: 6
|
|
|
I would tend to agree with John here - especially since the methods were previously abstract.
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12907
|
|
Ok, but it doesn't really make sense, or at least, it's not type safe. Suppose I'd write my own Calendar class, with an after(...) method that would be able to accept a Calendar as well as a Date. The most straightforward and type safe way would be to simply overload the after(...) method: If I want to do this with the current Calendar API, I'm forced to write my after(...) method in a non-OO and non-type safe way: Ugly!! Bad!!
|
 |
 |
|
|
subject: Why do Calendar.after(...) and before(...) take an Object?
|
|
|