当前位置: 技术问答>java相关
那么如何比较日期型的大小呢?
来源: 互联网 发布时间:2015-03-12
本文导语: | java.util.Date compareTo public int compareTo(Date anotherDate)Compares two Dates for ordering. Parameters: anotherDate - the Date to be compared. Returns: the value 0 if the argument Date is equal to this Date; a value less than 0 if this Date ...
|
java.util.Date
compareTo
public int compareTo(Date anotherDate)Compares two Dates for ordering.
Parameters:
anotherDate - the Date to be compared.
Returns:
the value 0 if the argument Date is equal to this Date; a value less than 0 if this Date is before the Date argument; and a value greater than 0 if this Date is after the Date argument
compareTo
public int compareTo(Date anotherDate)Compares two Dates for ordering.
Parameters:
anotherDate - the Date to be compared.
Returns:
the value 0 if the argument Date is equal to this Date; a value less than 0 if this Date is before the Date argument; and a value greater than 0 if this Date is after the Date argument
|
看看吧,API
before
public boolean before(Date when)
Tests if this date is before the specified date.
Parameters:
when - a date.
Returns:
true if and only if the instant of time represented by this Date object is strictly earlier than the instant represented by when; false otherwise.
--------------------------------------------------------------------------------
after
public boolean after(Date when)
Tests if this date is after the specified date.
Parameters:
when - a date.
Returns:
true if and only if the instant represented by this Date object is strictly later than the instant represented by when; false otherwise.
--------------------------------------------------------------------------------
equals
public boolean equals(Object obj)
Compares two dates for equality. The result is true if and only if the argument is not null and is a Date object that represents the same point in time, to the millisecond, as this object.
Thus, two Date objects are equal if and only if the getTime method returns the same long value for both.
Overrides:
equals in class Object
Parameters:
obj - the object to compare with.
Returns:
true if the objects are the same; false otherwise.
before
public boolean before(Date when)
Tests if this date is before the specified date.
Parameters:
when - a date.
Returns:
true if and only if the instant of time represented by this Date object is strictly earlier than the instant represented by when; false otherwise.
--------------------------------------------------------------------------------
after
public boolean after(Date when)
Tests if this date is after the specified date.
Parameters:
when - a date.
Returns:
true if and only if the instant represented by this Date object is strictly later than the instant represented by when; false otherwise.
--------------------------------------------------------------------------------
equals
public boolean equals(Object obj)
Compares two dates for equality. The result is true if and only if the argument is not null and is a Date object that represents the same point in time, to the millisecond, as this object.
Thus, two Date objects are equal if and only if the getTime method returns the same long value for both.
Overrides:
equals in class Object
Parameters:
obj - the object to compare with.
Returns:
true if the objects are the same; false otherwise.
|
他们返回都是布尔值,直接比较就行了
while ( gc1.before(gc2) ) {
// 操作
}
while ( gc1.before(gc2) ) {
// 操作
}
|
Date a=new Date("2001/12/31");
Date b=new Date("2002/1/1");
long p=a.getTime(),q=b.getTime();
//return q>p
Date b=new Date("2002/1/1");
long p=a.getTime(),q=b.getTime();
//return q>p