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