Compare two Timestamp in java
How can I compare if mytime
is between fromtime
and totime
:
Timestamp fromtime;
Timestamp totime;
Timestamp mytime;
Solution 1:
if(mytime.after(fromtime) && mytime.before(totime))
//mytime is in between
Solution 2:
Use the before
and after
methods: Javadoc
if (mytime.after(fromtime) && mytime.before(totime))
Solution 3:
From : http://download.oracle.com/javase/6/docs/api/java/sql/Timestamp.html#compareTo(java.sql.Timestamp)
public int compareTo(Timestamp ts)
Compares this Timestamp object to the given Timestamp object. Parameters: ts - the Timestamp object to be compared to this Timestamp object Returns: the value 0 if the two Timestamp objects are equal; a value less than 0 if this Timestamp object is before the given argument; and a value greater than 0 if this Timestamp object is after the given argument. Since: 1.4
Solution 4:
if (!mytime.before(fromtime) && !mytime.after(totime))