Java SQL Date Compare compareDates(Date date1, Date date2)

Here you can find the source of compareDates(Date date1, Date date2)

Description

Equivalent of compareTo() but actually works because Sun fucked up the JDK.

License

Open Source License

Declaration

public static int compareDates(Date date1, Date date2) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.sql.Timestamp;
import java.util.Date;

public class Main {
    /**//from  w  w w  .  ja v  a 2 s  . com
     * Equivalent of compareTo() but actually works because Sun
     * fucked up the JDK.  See:
     * 
     * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5103041
     * http://forum.hibernate.org/viewtopic.php?t=959124
     * http://forum.hibernate.org/viewtopic.php?t=925275
     * http://forum.hibernate.org/viewtopic.php?t=927602
     */
    public static int compareDates(Date date1, Date date2) {
        if (date1 instanceof Timestamp) {
            if (date2 instanceof Timestamp) {
                return date1.compareTo(date2);
            } else {
                return date1.compareTo(new Timestamp(date2.getTime()));
            }
        } else {
            if (date2 instanceof Timestamp) {
                return new Timestamp(date1.getTime()).compareTo(date2);
            } else {
                return date1.compareTo(date2);
            }
        }
    }
}

Related

  1. compareDate(java.sql.Date date1, java.sql.Date date2)
  2. compareDate(java.util.Date s1, java.util.Date s2)
  3. compareSqlDate(Object left, Object right)