Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do java.util.Calendar before() and after() methods take an Object as an argument, and simply return false if the passed Object is not a Calendar? [duplicate]

This was a bit of a WTF moment for me... I know the Java Date/Calendar classes are supposed to be horrible, but still why do this?

This just makes it easy to introduce subtle bugs, or am I missing something?

like image 892
Sonny Avatar asked Nov 24 '14 11:11

Sonny


1 Answers

I believe the original reason was that the methods were meant to be 'compatible' with the compareTo() method on the Comparable interface which specifies an Object argument (although this was obviously improved with the introduction of the generics syntax). Essentially before() is the same as compareTo(..) < 0 - and uses this to actually do the comparison internally.

Basically, it's a bad legacy thing though. Hopefully we can all move to the Java 8 libraries now and pretend Calendar never existed.

like image 172
BarrySW19 Avatar answered Oct 15 '22 12:10

BarrySW19