how to compare two times in android

android, java

Solution

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);
}

Problem

Two Dates are Comparing but Time is not comparing properly. This is my Code ``` final String setdate = cursor.getString(cursor.getColumnIndex(cursor.getColumnName(4))); final String settime = cursor.getString(cursor.getColumnIndex(cursor.getColumnName(5))); Calendar calendar1 = Calendar.getInstance(); SimpleDateFormat formatter1 = new SimpleDateFormat("dd/M/yyyy"); String currentDate = formatter1.format(calendar1.getTime()); Calendar calendar2 = Calendar.getInstance(); SimpleDateFormat formatter2 = new SimpleDateFormat("h:mm"); String currentTime = formatter2.format(calendar2.getTime()); if(currentDate.compareTo(setdate)>=0) { if(currentTime.compareTo(settime)>=0) { myCheckBox.setChecked(true); myCheckBox.setEnabled(false); } } ``` How can I compare two times.In database date and time field are different.So help me please.

Original source

Related problems