Home » Android » java – how to compare two times in android

java – how to compare two times in android

Posted by: admin June 15, 2020 Leave a comment

Questions:

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.

How to&Answers:
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);
}

Answer:

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

Answer:

Very Simple. Convert both the value time1 and time2 in millisecond or minute and measaure difference.

OR

This Link for function might help you. Good Luck.

Answer:

I think you’re asking how to compose a datetime from two separate fields of date and time. It’s pretty simple. You can use your SimpleDateFormat to do it:

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

    SimpleDateFormat sdf = new SimpleDateFormat("dd/M/yyyy h:mm");
    Date dbDate = sdf.parse(dateString + " " + timeString);

    int compareToNow = new Date().compareTo(dbDate);

Answer:

public static boolean isTimeGreater(String date, String time) {
    Date currentTime, pickedTime;
    SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm");
    String currentTimeString = sdf.format(new Date());

    try {
        currentTime = sdf.parse(currentTimeString);
        pickedTime = sdf.parse(date + " " + time);


        Log.e("Ketan", "Current Date: " + currentTime);
        Log.e("Ketan", "Picked Date: " + pickedTime);


        /*compare > 0, if date1 is greater than date2
        compare = 0, if date1 is equal to date2
        compare < 0, if date1 is smaller than date2*/

        if (pickedTime.compareTo(currentTime) > 0) {
            Log.e("Ketan", "Picked Date is Greater than Current Date");
            return true;
        } else {
            Log.e("Ketan", "Picked Date is Less than Current Date");
            return false;
        }
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return true;
}

Answer:

In Java there is a method which you can compare two date and time

For you have to format your date and time in DATE format in JAVA.

After that use below code

date1.compareTo(date2);

Hope above code will help you.

Let me know if you need more help from my side.