travega
travega

Reputation: 8415

Performance benefit java.util.Date compareTo vs before/after?

The title is pretty self explanatory but I wanted to know if there is any performance benefit to using

date1.compareTo(date2) < 0

instead of

date1.before(date2)

or

date2.after(date1)

in Java 7?

Upvotes: 2

Views: 1669

Answers (1)

Peter Lawrey
Peter Lawrey

Reputation: 533500

compareTo is slightly slower as it needs to return three possible results. Before and after is a simpler calculation. However, the best reason to use them is that they are clearer.

Upvotes: 3

Related Questions