how to compare two times in android

Try this:

private boolean checktimings(String time, String endtime) {

    String pattern = "HH:mm";
    SimpleDateFormat sdf = new SimpleDateFormat(pattern);

    try {
        Date date1 = sdf.parse(time);
        Date date2 = sdf.parse(endtime);

        if(date1.before(date2)) {
            return true;
        } else {

            return false;
        }
    } catch (ParseException e){
        e.printStackTrace();
    }
    return false;
}

Calendar calendar1 = Calendar.getInstance();
SimpleDateFormat formatter1 = new SimpleDateFormat("dd/M/yyyy h:mm");
String currentDate = formatter1.format(calendar1.getTime());

final String dateString = cursor.getString(4);
final String timeString = cursor.getString(5);
String datadb =dateString+" "+timeString;

//  Toast.makeText(context,"databse date:-"+datadb+"Current Date :-"+currentDate,Toast.LENGTH_LONG).show();

if(currentDate.compareTo(datadb)>=0) {
    myCheckBox.setChecked(true);
    myCheckBox.setEnabled(false);
}

Tags:

Java

Android