how to compare two times in android

2020-04-03 13:09发布

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.

标签: java android
6条回答
▲ chillily
2楼-- · 2020-04-03 13:16

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);
查看更多
Viruses.
3楼-- · 2020-04-03 13:19

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.

查看更多
Juvenile、少年°
4楼-- · 2020-04-03 13:25

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;
}
查看更多
不美不萌又怎样
5楼-- · 2020-04-03 13:30
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;
}
查看更多
唯我独甜
6楼-- · 2020-04-03 13:37
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);
}
查看更多
可以哭但决不认输i
7楼-- · 2020-04-03 13:41

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.

查看更多
登录 后发表回答