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
How the variable was built
teste
?– Isac
The test variable is in the same format as Cal.getTime() and was built from a Database
– Emanuel Sobreiro
Without the rest of the code it is difficult to help. Comparing dates through the
compareTo
of aDate
works well. See here. And you’d get the same result if you werecompareTo
of its ownCalendar
to compare between calendars– Isac
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
– Emanuel Sobreiro
Use the method
set
ofCalendar
. Something likecal.set(Calendar.HOUR_OF_DAY,0);
,cal.set(Calendar.MINUTE, 0);
andcal.set(Calendar.SECOND, 0);
– Isac
I have already edited with more code and the log. Thanks since.
– Emanuel Sobreiro
There’s only one way to tell if they’re the same:
Log.d(Tag,""+teste.getTime());
andLog.d(Tag,""+cal.getTime().getTime());
to see the representation in milliseconds of each one.– Isac
OK I’ll do it yet you’re there ?
– Emanuel Sobreiro
The result in milliseconds is different. How do I act? I will send the result. test = 1500678000000 cal.getTime()=1500678000198
– Emanuel Sobreiro
@Isac knows some way to solve the problem ?
– Emanuel Sobreiro
I can only help more once I know how the
cal
. Regardless of this one solution would be to pass theteste
for a calendar and compare each part withcal.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
, and the same for months and days– Isac
What I did was cal.set(Calendar.Milisecond,0); and it turned out
– Emanuel Sobreiro