I have a test:
@Test
public void testJodaStartDate2a() {
DateTime dateTime = new DateTime(2010,10,10,4,0, DateTimeZone.forID("America/Caracas"));
MutableDateTime mutableDateTime = dateTime.toMutableDateTime();
mutableDateTime.setDate(dateTime);
assertEquals(dateTime, mutableDateTime.toDateTime());
}
It’s straightforward, and in it I want to set the date part of dateTime value to the date part of dateTime value, which should be valid, and the assertion should succeed. However it fails.
As I can’t debug the methods of Joda, I created another test, which emulates what setDate() must do:
@Test
public void testJodaStartDate() {
DateTime dateTime = new DateTime(2010,10,10,4,0, DateTimeZone.forID("America/Caracas"));
long instantMillis = DateTimeUtils.getInstantMillis(dateTime);
Chronology instantChrono = DateTimeUtils.getInstantChronology(dateTime);
DateTimeZone zone1 = instantChrono.getZone();
if (zone1 != null) {
instantMillis = zone1.getMillisKeepLocal(DateTimeZone.UTC, instantMillis);
}
MutableDateTime mutableDateTime = dateTime.toMutableDateTime();
mutableDateTime.setMillis(mutableDateTime.getChronology().millisOfDay().set(instantMillis,
mutableDateTime.getMillisOfDay()));
assertEquals(dateTime, mutableDateTime);
}
This test is a copy of setDate()’s contents. As I have seen during debugging, at first, after
long instantMillis = DateTimeUtils.getInstantMillis(dateTime);
the instantMillis variables is assigned a value of 1286699400000 which is 2010-10-10 04:00 (VET) and 2010-10-10 08:30 (UTC).
After
instantMillis = zone1.getMillisKeepLocal(DateTimeZone.UTC, instantMillis);
the instantMillis is 1286683200000, which is 2010-10-10 00:00 (VET) and 2010-10-10 04:00 (UTC).
At assertion, the milliseconds value of mutableDateTime is 1286613000000 which is 2010-10-09 04:00 (VET) and 2010-10-09 08:30 (UTC)..
Isn’t this function just supposed to recalculate the date part of mutableDateTime to the date part of parameter value? What am I missing in the tests? I am very much appreciated.
You appear to have discovered a bug.
The following line is wrong — it should use the time zone that
thisis using.A quick search hasn’t found a current bug. I’ve filed one now (here).
Some tests I ran to check which time zones were problematic for this time. Basically it seems that the problem occurs when the date at given time zone is different to the date at UTC (at the same instant).