10

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.

silent_27
  • 303
  • 3
  • 17

6 Answers6

26

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;
}
Gustavo Morales
  • 2,614
  • 9
  • 29
  • 37
arshad shaikh
  • 673
  • 8
  • 11
9
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);
}
Finava Vipul
  • 998
  • 1
  • 11
  • 24
0

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.

iOSNoob
  • 1,420
  • 2
  • 16
  • 30
0

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);
Steve K
  • 4,863
  • 2
  • 32
  • 41
  • how to compare current date and current time first compare date than it's go to time look like if(dateString.compareTo(currdate)){ if(timeString.compareTo(cuuTime){ //code}} –  Feb 17 '15 at 05:27
  • A java `Date` is a timestamp. It encompasses both a date and a time. If you have two different fields for 'date' and 'time' in your database, you can turn both into a single Java `Date` using the `SimpleDateFormat` class as I did above. By default, creating a `new Date()` like I did above creates a `Date` instance set to the current system time. So the last line in my example compares the current system timestamp to the date you parsed from your database fields. – Steve K Feb 17 '15 at 05:31
  • 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; if(currentDate.compareTo(datadb)>=0) { myCheckBox.setChecked(true); myCheckBox.setEnabled(false); } –  Feb 17 '15 at 10:15
  • You should not use Strings to compare dates. You will find that, for instance, "01/12/2015" comes out as being LESS than "31/01/1979" – Steve K Feb 17 '15 at 22:37
0
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;
}
adiga
  • 34,372
  • 9
  • 61
  • 83
Ketan Ramani
  • 4,874
  • 37
  • 42
-1

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.

Vatsal Shah
  • 547
  • 3
  • 9