Java: Sort Date Using Comparator

Comparing Dates using the Comparator class in Java is not the same as comparing Strings. Even if you convert the Date’s getTime() method to a Long object, using compareTo() on those two Long objects will not do the trick. However, you can make use of the long values right away.

Check out the code.

Related Posts Plugin for WordPress, Blogger...

1 comment

  1. Hi,
    according to Suns/Oracles Java tutorial there is even an easier method to compare two dates:

    Comparator date_comparator = new Comparator() {
    public int compare(Date s1, Date s2){
    return st.compareTo(s2);

Leave a Reply

Your email address will not be published. Required fields are marked *