When I use java.util.Calendar to extract that year, month, day, hour, minute second, millisecond, ZONE_OFFSET, DST_OFFSET, AM_PM and HOUR (which is apparently different from hour) from each date object, these corresponding values are all equal. When I call java.util.Date.equals, this returns false. What other fields could be different that is causing java.util.Date.equals to return false?
(Disclaimer: I have not looked at that other thread since I posted in it. However I think it's your responsibility to combine the answers when you start a new thread like this.)
Siegfried Heintze wrote:HOUR (which is apparently different from hour)
Calendar.HOUR_OF_DAY really means that - the hour of day, from 0 to 23. Calendar.HOUR means the HOUR in AM/PM: 1-12. So if the HOUR_OF_DAY is 23, the HOUR is 11.
java.util.Date's equals method is implemented as follows in Java 6:
What are the values of your Calendars' getTimeInMillis() methods?
Date d = new Date();
Date e = new Date();
d.equals(e) <- no
If you want to compare the dates themselves, you have to check if the hour is the same, and the year is the same, and the second is the same, etc.
Otherwise get the milliseconds elapsed of each object and compare those values.
Alberto Pareja-Lecaros wrote:When you do a comparison of dates by object you are comparing the objects themselves, meaning everything about the object and their memory addresses.
No, that's not true at all. Read the documentation.
Alberto Pareja-Lecaros wrote:
This may return null, simply because the two constructors did not execute at exactly the same time. Or it may return true, because the execution times were close enough that they rounded to the same time in milliseconds. (A few years ago, most systems I worked on usually rounded to the nearest multiple of 10 milliseconds. I don't know how common that is now.)
Alberto Pareja-Lecaros wrote:If you want to compare the dates themselves, you have to check if the hour is the same, and the year is the same, and the second is the same, etc.
No. Just call equals(). A java.util.Date really has only one field, a long value equal to the number of milliseconds since the beginning of Jan 1, 1970, GMT. As Rob just showed. That's all that should need to be compared.
Alberto Pareja-Lecaros wrote:Otherwise get the milliseconds elapsed of each object and compare those values.
Exactly. So why waste time with the other field?
As Paul C said several times, the specific classes of the Date objects may be different. Often a database will return its own custom subclass of Date, which may not correctly obey the API set forth in java.util.Date.equals(). Or, another common situation is that one of your Date objects may have had its value rounded off at some point. For example, our Oracle system usually rounds dates to the nearest second. (Or truncates them; I forget.) I think that's standard behavior unless you declare the column as TIMESTAMP. So when you retrieve a value from the DB, the last three digits of getTime() are 000. If you want to compare dates that have been thus rounded, you can do some simple math to round both times accordingly.
Siegfried: I would suggest, next time you have two dates that you think shoudl be equal, but they're not: first use getClass() to identify what class each Date really is. Then print the value of getTime() for each date. You may well see that one or more millisecond values have been rounded off. Based on what you observe, you can round off both millisecond values, before comparing them.
I get the green bar on these two tests. I don't understand why. For some reason, when I explicitly set all the fields in cal1, I get strange values.
Siegfried Heintze wrote:On lines 88 and 90, why are not the dates d2 and d4 equal?
I get the green bar on these two tests.
I don't understand. Lines 88 and 90 assert that the dates are equal. And you say you get a green bar for both tests. For most of us, a green bar means that the tests passed, and all assertions were true. So, why are you saying d2 and d4 are not equal? Did the tests pass, or not? Were you running some other code, different than what you've shown?
You may find the issue to be in the hour. You set the hour of cal1 to 15 first. Then you copy that to cal2. Then you set the hour of cal1 to 10 PM. Of course d2 and d4 are not going to be the same.
Thanks Rob. I think that specifying the timezone in the java.util.calendar.getinstance fixed that problem above. I think I am now having a different problem. The UT (unit tests) pass for about an hour and then the test bellow starts failing. Sounds like a time zone problem to me! What do you think?
Please scoll all the way down to the bottom of the last listing and examine line 35. It keeps failing here (after an hour) because the values for variable "h" keep changing! Its driving me nuts!
Here is my Make Gregorian class I use to insert and extract parameters into/outof java.util.dates. I'm setting as many of the fields as I know how.
Here is my main test class
Previously, in desperation, I printed out a bunch of fields and then defined the values as constants and started setting them as exemplified on line 89. If I comment out line 89 it passes.
Hmmm.... Well daylight savings time does not end until late october and I got the value of the offset from printing it out. Now when I print it out it is zero.
So I if I am setting the locale and timezone in the constructor, is it redundant to specify them on line 90? Should I comment out line 91 too? I guess so since I am using the 24 hour notation instead of AM/PM. But I'm setting HOUR_OF_DAY, now HOUR so why does it matter that am setting AM_PM? Well it does. Is this a bug?