I have a problem with the comparing two DateTime objects.
System.out.println(response.getCreationDate().toString()); // will return "2013-12-31T22:59:21.000+01:00", but...
assertThat(response.getCreationDate(), equalTo(new DateTime("2013-12-31T22:59:21+01:00"))); // will throw an assertation error with the following error
Expected: <2013-12-31T22:59:21.000+01:00>
but: was <2013-12-31T22:59:21.000+01:00>
Anyone has an idea what am I missing here?
Btw, if you are wondering why the DateTime is displayed in GMT+1:00 zone, cause that's the timezone in which I want my DateTime objects to be by default.
Thanks!
DateTime
inherits its equals
method from AbstractInstant
. It is implemented as such
public boolean equals(Object readableInstant) {
// must be to fulfil ReadableInstant contract
if (this == readableInstant) {
return true;
}
if (readableInstant instanceof ReadableInstant == false) {
return false;
}
ReadableInstant otherInstant = (ReadableInstant) readableInstant;
return
getMillis() == otherInstant.getMillis() &&
FieldUtils.equals(getChronology(), otherInstant.getChronology());
}
Notice the last line comparing chronology. It's possible your instances' chronologies are different.
This code (example) :
Chronology ch1 = GregorianChronology.getInstance();
Chronology ch2 = ISOChronology.getInstance();
DateTime dt = new DateTime("2013-12-31T22:59:21+01:00",ch1);
DateTime dt2 = new DateTime("2013-12-31T22:59:21+01:00",ch2);
System.out.println(dt);
System.out.println(dt2);
boolean b = dt.equals(dt2);
System.out.println(b);
Will print :
2013-12-31T16:59:21.000-05:00
2013-12-31T16:59:21.000-05:00
false
You are probably comparing two DateTime s with same date but different Chronology .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.