Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joda DateTime equals vs isEqual

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.

like image 541
Sachin Verma Avatar asked Jul 14 '15 07:07

Sachin Verma


2 Answers

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,

  1. 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)

  2. If you only want to compare the mills and time zone. Then I guess you could just use isEqual method.

like image 103
kucing_terbang Avatar answered Sep 28 '22 16:09

kucing_terbang


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 Chronologys.

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).

like image 20
Yosef Weiner Avatar answered Sep 28 '22 14:09

Yosef Weiner