How can I prevent Joda Time by throwing an exception during the Brazilian DST transition

Today, our Brazilian users generate a lot of crash reports for us. I tracked it down to this code that 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); } } 

An 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 am using Java 1.8.0_60 on OS X 10.11 with Joda Time 2.8.2.

What work will allow me to correctly get a DateTime instance representing the time in the current day that will be shifted in milliseconds after the start of the day?

+6
source share
1 answer

Do not go through 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); 
+3
source

All Articles