Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is 1942/4/3 00:00:00 an illegal date in java.util.Calendar?

Is there something special in the date 3rd of April 1942? For some reason the the hour of day 0 (12:00 am) is illegal for this specific date. The date is accepted when a lenient calendar is used but the hour of day is incremented to 1 (1:00 am).

Relevant code

java.util.Calendar calendar = java.util.Calendar.getInstance(
     java.util.TimeZone.getTimeZone("Europe/Helsinki")
);
calendar.clear();
calendar.setLenient(false);
calendar.set(1942, 3, 3, 0, 0, 0);
calendar.getTimeInMillis();

Exception

java.lang.IllegalArgumentException: HOUR_OF_DAY
 at java.util.GregorianCalendar.computeTime(Unknown Source)
 at java.util.Calendar.updateTime(Unknown Source)
 at java.util.Calendar.getTimeInMillis(Unknown Source)

I'd really prefer is the dates were not lenient as I don't want to accept impossible dates.

-- edit

As the accepted answer and many of the comments pointed out, this does indeed relate to daylight saving. On the 3rd of April 1942 at 00:00 daylight saving was tested in EEST/Helsinki timezone. Currently, daylight savings has been in use since 1981 and the clock is wound forward at 03:00 instead of 00:00. This means that e..g 28th of March 2010 03:00 does no exists in java.util.Calendar.

I'll just have to create a special case for this specific date in my code.

like image 891
Aleksi Yrttiaho Avatar asked Jan 21 '23 21:01

Aleksi Yrttiaho


2 Answers

Just a guess, but is it related to daylight savings? I guess that would depend on the localization of your codebase (for the exact dates of daylight savings coming into effect) as well as other factors.

---EDIT--- Maybe not, since that was a Friday.

like image 135
ZombieSheep Avatar answered Feb 21 '23 22:02

ZombieSheep


Timezones and daylight savings varied year by year, and 1942 is a particularly odd one in much of the world - various parts of were changing hands rather rapidly. Some parts of Asia were standardised to 'Burma Time' so transitioned from local times.

Your profile says you're in Finland, which was not so complicated. This world-time database puts the date and time in question as being within one hour lost to normal daylight saving.

like image 22
Pete Kirkham Avatar answered Feb 21 '23 22:02

Pete Kirkham