Jodatime:由于时区偏移转换而导致的非法时刻(夏令时“间隙”)

时间:2019-04-03 05:45:40

标签: android jodatime

此崩溃发生在2天前,我不明白为什么。一切都已经完美运行了一年:

Fatal Exception: org.joda.time.IllegalFieldValueException Value 2 for hourOfDay is not supported: Illegal instant due to time zone offset transition (daylight savings time 'gap'): 2019-03-31T02:09:00.000 (Europe/Paris)

我的代码:

DateTime dtToRefresh = mDateTime != null ? mDateTime : DateTime.now();
                        dtToRefresh = dtToRefresh.secondOfMinute().setCopy(0);
                        dtToRefresh = dtToRefresh.millisOfSecond().setCopy(0);
                        dtToRefresh = dtToRefresh.hourOfDay().setCopy(hourOfDay);
                        dtToRefresh = dtToRefresh.minuteOfHour().setCopy(minute);

崩溃始于:

dtToRefresh = dtToRefresh.hourOfDay().setCopy(hourOfDay);

“ hourOfDay”是距日期时间选择器一小时的时间(经典过程)。

您有一些调查方法吗?

非常感谢您!

1 个答案:

答案 0 :(得分:1)

对于巴黎,夏令时于3月31日开始,因此从这一天开始,您的代码就会崩溃。因此,您没有实现支持时间的代码。

如果您手动计时,则意味着您需要为该时区实施DST时间。如果要花费系统时间,就没有问题。

解决方案:

赶上异常,尝试增加1小时或减去1小时(适用)。

 try 
 {
   DateTime dtToRefresh = mDateTime != null ? mDateTime : DateTime.now();
                    dtToRefresh = dtToRefresh.secondOfMinute().setCopy(0);
                    dtToRefresh = dtToRefresh.millisOfSecond().setCopy(0);
                    dtToRefresh = dtToRefresh.hourOfDay().setCopy(hourOfDay);
                    dtToRefresh = dtToRefresh.minuteOfHour().setCopy(minute);
 } 
 catch (IllegalArgumentException iae) 
 { 
    dttoRefresh.plusHours(1);
 }