java.util.Calendar – Year 1947

I found an interesting behavior in WSO2 platform using Calendar to convert String to Date. WSO2 uses java.util.Calendar as the final result. I found that if a date from 01.01.1947 – 23.02.1947 is used in code snippet bellow the result always gives us one day before. I found out that its our Timezone doing this as it is one hour forward. Meaning Calendar decreases result one hour backwards and we get the day before 11PM.

We did already solve the problem by not converting to Date. So that is not the issue.

The question is, why is this happening only for dates 01.01.1947 – 23.02.1947?

Here is an example code snippet that can simulate the issue:

    int year = 1947;
    int month = 2;
    int day = 23;
    int timezoneOffset = TimeZone.getDefault().getRawOffset();
    Calendar calendar = Calendar.getInstance();
    calendar.clear();
    calendar.set(1, year);
    calendar.set(2, month - 1);
    calendar.set(5, day);
    calendar.set(15, timezoneOffset);

    System.out.println(calendar.getTime());

In my timezone the result of timezoneOffset is 3600000 in case you want to test my timezone (Czech Republic).

As a note this is a code written in WSO2, i am unable to change it in case there are questions about that.

Version of Java i am using for this is: openjdk 11.0.8 2020-07-14

Link: WSO2 platform

Answer

On February 23 1947 the Czech Republic (Czechoslovakia, at the time) passed from the GMT timezone to the CET one, so clocks went 1 hour ahead. The opposite happened on December 1 1946 (CET->GMT, -1 hour).

This happened because the local government at the time implemented Winter Time, for the same reason we usually have DST, that is to (supposedly) save on energy consumption.

Link: Time Changes in Prague Over the Years on timeanddate.com

Leave a Reply

Your email address will not be published. Required fields are marked *