0

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".

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Antegeia
  • 271
  • 3
  • 18

2 Answers2

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