I've got two similar dates and I am doubtful about the comparison Joda DateTime api provides:
log.info("comparing:"+abrDateTime+": and :"+breakStart+":"+abrDateTime.equals(breakStart));
this prints
comparing:2015-07-14T12:25:47.000+05:30: and :2015-07-14T12:25:47.000+05:30:false
While with DateTime.isEqual
log.info("comparing:"+abrDateTime+": and :"+breakStart+":"+abrDateTime.isEqual(breakStart));
prints:
comparing:2015-07-14T12:25:47.000+05:30: and :2015-07-14T12:25:47.000+05:30:true
Problem is I'm using DateTime.equals method everywhere in my app, should I change it to isEqual
?
NOTE: Both instances got same same millis and same timezone as you can see in prints.
First, you may need to check your code, because the snippets that you give is printing the same object (which is abrDateTime
), yet, in the equals
and isEqual
method, you compare the abrDateTime
with breakStart
object.
Second, at time like this, the best way is to check the API documentation. for equals, the doc state that :
Compares this object with the specified object for equality based on the millisecond instant, chronology and time zone
and the isEqual, the doc state that :
Is this instant equal to the instant passed in comparing solely by millisecond
So, the choice is , do you want to check the time only based on its time? then go with isEqual
method. Or, if you want to check also the timezone and chronology, then go with equals
EDIT:
Addressing your comment,
Chronology and why it's needed to compare => Taken from the doc
Chronology provides access to the individual date time fields for a chronological calendar system.
Therefore, you can think that Chronology
as a class which contain the date and time fields for a specific calendar system. (such as, ISO, Gregorian, Buddhist, etc.)
It is needed, because you don't want to compare two date but in different calendar system. example, let say, in islamic calendar system, first of June in gregorian would be 14 SHa`baan (this is my opinion so it might be different from what the API author intended as the reason is not documented)
If you only want to compare the mills and time zone. Then I guess you could just use isEqual
method.
The equals
method of ReadableInstant
(superclass of DateTime
) also incorporates the Chronology
, while isEqual
strictly looks at the instant in time (milliseconds since epoch). In your case, the two objects have different Chronology
s.
From the Javadocs:
Compares this object with the specified object for equality based on the millisecond instant and the Chronology. All ReadableInstant instances are accepted.
To compare two instants for absolute time (ie. UTC milliseconds ignoring the chronology), use isEqual(ReadableInstant)or Comparable.compareTo(Object).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With