1

Possible Duplicate:
How to calculate time difference in java?

I have two Strings "10:00:00" and "14:00:00". I have converted them to Date Format. Now i want two compare the both time.Can anyone suggest me what to do....

Community
  • 1
  • 1
user1549971
  • 119
  • 2
  • 5

3 Answers3

5

use compareTo().

date1.compareTo(date2);

from java docs:

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.

other examples

Thousand
  • 6,562
  • 3
  • 38
  • 46
2

Check Date#equals, Date#after, Date#before

I preferrably use Calendar class for date related task.. It is quite to use them in some context.. You can find more helpful examples in this link

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
0

Before you do the comparison, as the timestamps are still in the String format, you need to convert them to java.util.Date and the simplest way is to assign an arbitrary date part to both time strings (e.g. concatenate both time strings to "1/1/1970 "), then convert them into Date using SimpleDateFormat, then get time in milliseconds and do the comparison.

amphibient
  • 29,770
  • 54
  • 146
  • 240