Comparison of dates is wrong

0

So I have this code that compares a date with today's date, but sometimes the dates are as follows:

   D/tag: Sat Jul 22 00:00:00 GMT+01:00 2017 <<-Data indicada
   D/tag: Sat Jul 22 18:57:17 GMT+01:00 2017 <<-Data de hoje

And I want you to do this, the program does not return that the indicated date is smaller or it is a date earlier than today, I want if the date is today that is indicated as not being before today's date . My code that compares is as follows:

  if(teste!=null){
            caldroidFragment.setBackgroundDrawableForDate(cyan, teste);
            DatesList.add(teste);
            int ListSize = DatesList.size();
             if(cal.getTime().compareTo(teste)>0){
                 caldroidFragment.setBackgroundDrawableForDate(red, teste);
             }
        }

In this example I indicated this to return 1. Cal.getTime () is the present tense. "test" is the date indicated by the user.

My code is as follows:

    for(int i=1;i <= myDB.getLastId();i++){

        cal.set(Calendar.HOUR_OF_DAY,0);
        cal.set(Calendar.MINUTE,0);
        cal.set(Calendar.SECOND,0);

        String dt = myDB.getDates(i);
            java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("dd-MM-yyyy");
            Date teste = null;

            try {
                teste = sdf.parse(dt);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        if(teste!=null){
            Log.d(Tag,""+teste);
            Log.d(Tag,""+cal.getTime());
            Log.d(Tag,""+cal.getTime().compareTo(teste));
            caldroidFragment.setBackgroundDrawableForDate(cyan, teste);
            DatesList.add(teste);
            int ListSize = DatesList.size();
             if(cal.getTime().compareTo(teste)>0){
                 caldroidFragment.setBackgroundDrawableForDate(red, teste);
             }
             else if(cal.getTime().compareTo(teste)==0){
                 caldroidFragment.setBackgroundDrawableForDate(white,teste);
             }
        }



    }

The log is giving:

    Sat Jul 22 00:00:00 GMT+01:00 2017
    Sat Jul 22 00:00:00 GMT+01:00 2017
    1

I do not understand why cal.getTime (). compareTo (test) is returning 1 when the values are clearly the same! Thanks

    
asked by anonymous 22.07.2017 / 20:02

1 answer

1

When you compare two objects of type Date , you are actually comparing the representation in milliseconds of these objects.

The Date.compareTo(Date anotherDate) method is implemented as follows:

public int compareTo(Date anotherDate) {
    long thisTime = getMillisOf(this);
    long anotherTime = getMillisOf(anotherDate);
    return (thisTime<anotherTime ? -1 : (thisTime==anotherTime ? 0 : 1));
}

Notice that it calls getMillisOf makes the comparison using the representation in milliseconds of the two Date objects.

At the Log.d(Tag,""+cal.getTime()); line, you printa on the console a Date with the format EEE MMM dd HH:mm:ss zzz yyyy

Note that in the format, the milliseconds field is ignored. So when you printou the Date , you were not seeing your full rendering.

To solve your problem you can:

Clear the two calendars, minutes, seconds, and milliseconds fields.

cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);

Note that since you created the date teste with sdf.parse (dt) passing only dd-MM-yyyy , the hour, minute second, and millisecond fields will already be zeroed.

In this way, cal.getTime().compareTo(teste)>0 scan should work.

Compare each field of objects Calendar (more laborious)

if(teste!=null){
    Calendar testeCal = Calendar.getInstance();
    testeCal.setTime(teste);

    caldroidFragment.setBackgroundDrawableForDate(cyan, teste);
    DatesList.add(teste);
    int ListSize = DatesList.size();

    if(cal.get(Calendar.DAY_OF_MONTH) == testeCal.get(Calendar.DAY_OF_MONTH) &&
        cal.get(Calendar.MONTH) == testeCal.get(Calendar.MONTH) &&
        cal.get(Calendar.YEAR) == testeCal.get(Calendar.YEAR)) {
        ...
    }

}

This approach also works, but it becomes more laborious to make comparisons of larger or smaller dates.

    
23.07.2017 / 16:16