Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare two Timestamp in java

Tags:

java

timestamp

How can I compare if mytime is between fromtime and totime:

Timestamp fromtime; Timestamp totime;  Timestamp mytime; 
like image 391
user620130 Avatar asked Oct 27 '11 08:10

user620130


People also ask

How do I compare two time stamps in Java?

The compareTo() method of Timestamp class returns : Integer value 0 if this Timestamp object is equal to given Timestamp object. A value less than 0 if this Timespan object is before the given argument. A value greater than 0 if this Timespan object is after the given argument.

Can you compare LocalDateTime in Java?

The recommended way to compare two LocalDateTime objects is using provided methods which compare both date-time parts and return a boolean value – true or false . These methods only consider the position of the two dates on the local timeline and do not consider the chronology, or calendar system.

Can we compare two timestamps in SQL?

To calculate the difference between the timestamps in MySQL, use the TIMESTAMPDIFF(unit, start, end) function. The unit argument can be MICROSECOND , SECOND , MINUTE , HOUR , DAY , WEEK , MONTH , QUARTER , or YEAR . To get the difference in seconds as we have done here, choose SECOND .


2 Answers

if(mytime.after(fromtime) && mytime.before(totime))   //mytime is in between 
like image 75
legendofawesomeness Avatar answered Sep 21 '22 21:09

legendofawesomeness


Use the before and after methods: Javadoc

if (mytime.after(fromtime) && mytime.before(totime)) 
like image 20
Jean Logeart Avatar answered Sep 22 '22 21:09

Jean Logeart