Today our Brazilian users are generating plenty of crash reports for us. I've tracked it down to this code, which throws a Joda exception:
import org.joda.time.DateTime;
import org.joda.time.DateTimeUtils;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalTime;
public class ScratchSpace {
public static void main(String[] args) {
// force Joda to act like we are in Sao Paolo on 2015-10-18
DateTimeUtils.setCurrentMillisFixed(1445185758078L); // 2015-10-18T18:29
DateTimeZone.setDefault(DateTimeZone.forID("America/Sao_Paulo"));
// most of users have offset == 0, but it could be any number of millis from 0 to 86_400_000-1 (millis in day)
int offset = 0;
// local time at start of day + offset millis
final LocalTime localTime = LocalTime.fromMillisOfDay(offset);
// convert to a time on the current day
DateTime dateTime = localTime.toDateTimeToday(); // throws org.joda.time.IllegalFieldValueException exception
System.out.println("dateTime = " + dateTime);
}
}
The exception:
Exception in thread "main" org.joda.time.IllegalFieldValueException: Value 0 for hourOfDay is not supported: Illegal instant due to time zone offset transition (daylight savings time 'gap'): 2015-10-18T00:29:18.078 (America/Sao_Paulo)
at org.joda.time.chrono.ZonedChronology$ZonedDateTimeField.set(ZonedChronology.java:486)
at org.joda.time.chrono.BaseChronology.set(BaseChronology.java:240)
at org.joda.time.LocalTime.toDateTimeToday(LocalTime.java:1287)
at org.joda.time.LocalTime.toDateTimeToday(LocalTime.java:1270)
I'm using Java 1.8.0_60 on OS X 10.11 with Joda Time 2.8.2.
What work-around will allow me to correctly get a DateTime instance representing a time on the current day that is offset milliseconds after the start of the day?
Don't go though a LocalTime. Create a DateTime, and add the offset in milliseconds:
DateTime startOfDay = LocalDate.now().toDateTimeAtStartOfDay();
DateTime dateTime = startOfDay.plus(offset);
System.out.println("dateTime = " + dateTime);
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