I am trying to create a scheduler in android. Can anyone please help me out on how i can compare a given time with current time? For example current_time = "14:00:00".
Asked
Active
Viewed 2,606 times
2 Answers
1
Try this code
String yourTime = "14:00:00";
//get your today date as string
String today = (String) android.text.format.DateFormat.format(
"hh:mm:ss", new java.util.Date());
//Convert the two time string to date formate
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
Date date1 = sdf.parse(yourTime);
Date date2 = sdf.parse(today);
//do the comparison
if (date1.before(date2)) {
//do something
}

FinalDark
- 1,748
- 1
- 16
- 26
0
you can try it using compareTo()
method which
Compare the receiver to the specified Date to determine the relative ordering.
Date selectedDate= new Date(selectedMilli);
Date currentDate= new Date();
int i = selectedDate.compareTo(currentDate);
Parameters
date a Date to compare against.
Returns
an int < 0 if this Date is less than the specified Date, 0 if they are equal, and an int > 0 if this Date is greater.

Ketan Ahir
- 6,678
- 1
- 23
- 45