Date comparison goes wrong

Asked

Viewed 46 times

0

So I have this code that compares a date to 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 that by doing this, the program does not return that the date indicated is shorter or is an earlier date than today, I want if today’s date is indicated as not being earlier than 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);
             }
        }

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

My code is this: :

    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 don’t understand why cal.getTime(). compareTo(test) is returning 1 when the values are clearly equal! Thank you

  • 1

    How the variable was built teste ?

  • The test variable is in the same format as Cal.getTime() and was built from a Database

  • 1

    Without the rest of the code it is difficult to help. Comparing dates through the compareTo of a Date works well. See here. And you’d get the same result if you were compareTo of its own Calendar to compare between calendars

  • Yes it is working well , but what I want is to do Calendar.getTime() to get, the date of the moment , but wanted the minutes and seconds hours to stay at 0 and instead of Sat Jul 22 18:57:17 GMT+01:00 2017 would stay Sat Jul 22 00:00:00 GMT+01:00 2017

  • 1

    Use the method set of Calendar. Something like cal.set(Calendar.HOUR_OF_DAY,0); , cal.set(Calendar.MINUTE, 0); and cal.set(Calendar.SECOND, 0);

  • I have already edited with more code and the log. Thanks since.

  • There’s only one way to tell if they’re the same: Log.d(Tag,""+teste.getTime()); and Log.d(Tag,""+cal.getTime().getTime()); to see the representation in milliseconds of each one.

  • OK I’ll do it yet you’re there ?

  • The result in milliseconds is different. How do I act? I will send the result. test = 1500678000000 cal.getTime()=1500678000198

  • @Isac knows some way to solve the problem ?

  • I can only help more once I know how the cal. Regardless of this one solution would be to pass the teste for a calendar and compare each part with cal.get(Calendar.YEAR) == cal2.get(Calendar.YEAR), and the same for months and days

  • 1

    What I did was cal.set(Calendar.Milisecond,0); and it turned out

Show 7 more comments

1 answer

1


When you compare two objects of the type Date, in fact you’re comparing the millisecond representation of these objects.

The method Date.compareTo(Date anotherDate) 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 he calls getMillisOf makes the comparison using the millisecond representation of the two objects Date.


On the line Log.d(Tag,""+cal.getTime());, you printa on console a Date 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 weren’t seeing your full representation.


To solve your problem you can:

Reset minutes, seconds and milliseconds fields of the two calendars (easiest).

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

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

Thus, the verification cal.getTime().compareTo(teste)>0 should work.

Compare each object field 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.