Why does converting a Joda DateTime to a calendar and vice versa change the centuryOfEra field and nothing else?

I fiddled with JodaTime today and I expected this test to pass:

@Test public void dateTimeShouldRoundTrip() { DateTime originalDateTime = new DateTime(2013, 7, 4, 0, 0); DateTime roundTrip = new DateTime(originalDateTime.toGregorianCalendar()); assertThat(roundTrip, is(originalDateTime)); } 

But that fails. I tested it under JodaTime 2.1 and 2.2.

A further check (using SamePropertyValuesAs matches) shows that the failure is caused by the difference in the centuryOfEra property:

 originalDateTime.getCenturyOfEra(); // 20 roundTrip.getCenturyOfEra(); // 21 

So, why does this property change when everything else - year, month, day, day of the week, time zone, etc. - not? This is mistake? Can't you round DateTime to calendar and back?

+8
java jodatime
source share
1 answer

Joda Time version 2.2 has an isEqual method that compares only a millisecond, while equals compares a millisecond, timeline, and time zone:

 DateTime d = new DateTime(); new DateTime(d.toGregorianCalendar()).isEqual(d); // returns true new DateTime(d.toGregorianCalendar()).equals(d); // returns false, as you observed 

Therefore, the timeline or time zone is not saved by conversion to or from GregorianCalender . Leave a comment if you want me to research further.

+1
source share

All Articles