JATIN DEVANI JATIN DEVANI - 7 months ago 29
Java Question

how to compare two Time in android

Two Date Comparing but Time is not Compare
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 to i compare two time .in database date and time field are diffrent so help me plz

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