Java Date timezone printing different timezones for different years, Workaround needed

Mritunjay :

While testing my application I got a weird problem. When I put a date having the year before 1945, it changes the timezone.

I have got this simple program to show the problem.

public static void main(String[] args) {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ");
    Calendar calendar = Calendar.getInstance();

    System.out.println("**********Before 1945");
    calendar.set(1943, Calendar.APRIL, 12, 5, 34, 12);
    System.out.println(format.format(calendar.getTime()));
    System.out.println(calendar.getTime());

    System.out.println("**********After 1945");
    calendar.set(1946, Calendar.APRIL, 12, 5, 34, 12);
    System.out.println(format.format(calendar.getTime()));
    System.out.println(calendar.getTime());
}

The output I am getting is below:-

**********Before 1945
1943-04-12 05:34:12+0630
Mon Apr 12 05:34:12 IDT 1943

**********After 1945
1946-04-12 05:34:12+0530
Fri Apr 12 05:34:12 IST 1946

For the first one, I am getting it as +0630 and IDT, while for the second one, I am getting +0530 and IST which is expected.

Edit:-

After looking at @Elliott Frisch answer I tried a date before 1942:-

calendar.set(1915, Calendar.APRIL, 12, 5, 34, 12);
System.out.println(format.format(calendar.getTime()));
System.out.println(calendar.getTime());

output:-

1915-04-12 05:34:12+0553
Mon Apr 12 05:34:12 IST 1915

Here again, it says IST but shows +0553. Shouldn't it be +0530.

Just for a comparison, I tried same thing in javascript:-

new Date("1946-04-12 05:34:12") //prints Fri Apr 12 1946 05:34:12 GMT+0530 (IST)
new Date("1943-04-12 05:34:12") //prints Fri Apr 12 1943 05:34:12 GMT+0530 (IST)
new Date("1915-04-12 05:34:12") //prints Mon Apr 12 1915 05:34:12 GMT+0530 (IST)

Which works fine. I want to know why java is affected by it, and if it's a known problem, what is the possible workaround for it.

Thanks in advance.

Tomas Langkaas :

This is likely the expected behaviour from Java (and not from JavaScript).

As implied by the comment by RobG above, programming languages may or may not support historical time rules (such as DST and timezone offsets). In your case, it appears that your Java runtime supports it, whereas your JavaScript runtime does not.

A list of historical timezones and DST rules for India can be found at timeanddate.com. The list confirms the timezone offsets of your Java dates:

  • Until 1941: UTC+5:53:20
  • 1941: UTC+6:30
  • 1942: UTC+5:30
  • 1943-44: UTC+6:30
  • From 1945: UTC+5:30

Checking your dates against Wolfram|Alpha further confirms your Java date UTC offsets: 1915, 1943, 1946

Wikipedia provides more information about time in India:

Calcutta time was officially maintained as a separate time zone until 1948

Calcutta time could either be specified as UTC+5:54 or UTC+5:53:20. The latter is consistent with your code example.

The Wikipedia entry further informs that the current IST timezone with an offset of UTC+5:30 was not in full effect in all of India until 1955.

As pointed out by Elliott Frisch and confirmed by the link to timeanddate.com above, DST was in effect during WWII. In your comment to his answer, you state:

is this the way we are supposed to save in database and use it in applications, or we use some workaround for it

I guess it depends. If you really need to distinguish dates as points in time accurately, you would need a timezone-independent representation such as UTC or unix time (or milliseconds since the unix epoch). If you just work with local dates from the same timezone, a simple string representation (e.g. YYYY-MM-DD hh:mm:ss) could suffice.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=448190&siteId=1