Jodatime:由于时区偏移过渡而造成的非法瞬间(夏令时'缺口')

问题描述 投票:0回答:1

这次事故发生在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”是日期时间选择器(经典流程)的一小时。

你有一些调查方法吗?

非常感谢你!

android jodatime
1个回答
1
投票

对于巴黎夏令时从3月31日开始,所以从那天开始你的代码崩溃了。因此,您没有实现代码来支持dst时间。

如果您手动占用时间意味着您需要为该时区实施DST时间。如果采取系统时间意味着没有问题。

解:

捕获异常并尝试添加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);
 }
© www.soinside.com 2019 - 2024. All rights reserved.