Wrong offset for TimeZone casablanca (java)

2019-06-25 14:09发布

I am trying to build a date (29/07/2014 at 02:55:08 am) in Casablanca timezone and got this exception:

Exception in thread "main" java.lang.IllegalArgumentException: HOUR_OF_DAY: 2 -> 3 at java.util.GregorianCalendar.computeTime(Unknown Source)

Calendar cal = GregorianCalendar.getInstance(TimeZone.getTimeZone("Africa/Casablanca"));
cal.setLenient(false);
cal.set(Calendar.DATE, 29);
cal.set(Calendar.MONTH, 6); // July
cal.set(Calendar.YEAR, 2014);
cal.set(Calendar.HOUR_OF_DAY, 2);
cal.set(Calendar.MINUTE, 55);
cal.set(Calendar.SECOND, 8);
cal.getTime();

The exception is thrown with jre 1.7.0_71 and 1.8.0_20 , but not with 1.6.0_30.

As far as I know, there is no daylight saving change occuring at that particular time. Any idea ?

Thanks!

标签: java timezone
2条回答
趁早两清
2楼-- · 2019-06-25 14:36

It actually does correspond to a daylight savings change. In Morocco, daylight savings is suspended during Ramadan. See http://www.timeanddate.com/news/time/egypt-morocco-dst-ramadan-2014.html for more information.

The timezone data entry for 2014 is as follows:

Rule    Morocco 2014    only    -       Jun     28       3:00   0       -
Rule    Morocco 2014    only    -       Aug      2       2:00   1:00    S
查看更多
在下西门庆
3楼-- · 2019-06-25 14:56

The JRE is not always updated with the latest time zone data. To stay current, you must use the TZUpdater utility. Oracle publishes the list of time zone updates for both JRE and TZUpdater. It shows that the last JRE update (as of my writing this) was in release 65 for Java 7, and release 11 for Java 8 - and it was made with version 2014c of tzdata.

If we look at the sources for tzdata at version 2014c, we can see that the guess at that time for Morocco did indeed guess the Ramadan DST suspension from Jun 29 - Jul 29.

Rule    Morocco 2014    only    -   Jun  29      3:00   0       -
Rule    Morocco 2014    only    -   Jul  29      2:00   1:00    S

It was of course later updated to the values shown in Chris's answer, when Egypt finally announced the real dates.

You can see some of the breadcrumbs from the original guesswork here and there are many discussions in the tz list archives about Egypt, starting in May and continuing through to July.

查看更多
登录 后发表回答