1.调用方式
oneDate.compareTo(anotherDate);
2.函数源码
/**
* Compares two Dates for ordering.
*
* @param anotherDate the <code>Date</code> to be compared.
* @return the value <code>0</code> if the argument Date is equal to
* this Date; a value less than <code>0</code> if this Date
* is before the Date argument; and a value greater than
* <code>0</code> if this Date is after the Date argument.
* @since 1.2
* @exception NullPointerException if <code>anotherDate</code> is null.
*/
public int compareTo(Date anotherDate) {
long thisTime = getMillisOf(this);
long anotherTime = getMillisOf(anotherDate);
return (thisTime<anotherTime ? -1 : (thisTime==anotherTime ? 0 : 1));
}
我们看到,判断的方式非常简单,就是根据时间的Long值进行判断的,如果Long值大就返回1,相等返回0,小的话返回-1。