Strange Java Timezone Date Conversion Problem

I want to convert ms-since-1970-timestamp to a date with timezone (Germany).

Here are two variants of code which worked - at least, I remember using it and it worked:

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;

public class TestDate {

    public static void main(String[] args) {
        Calendar cal = GregorianCalendar.getInstance(TimeZone.getTimeZone("Germany"), Locale.GERMANY);

        Date d = new Date();
        cal.setTime(d);

        System.out.println(String.format("%02d.%02d.%04d %02d:%02d:%02d", 
                cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.MONTH)+1, cal.get(Calendar.YEAR),
                cal.get(Calendar.HOUR_OF_DAY),  cal.get(Calendar.MINUTE),  cal.get(Calendar.SECOND)));

        SimpleDateFormat df = new SimpleDateFormat( "dd.MM.yyyy HH:mm:ss.S" );
        df.setTimeZone(TimeZone.getTimeZone("Germany"));
        System.out.println(df.format(d));
    }

}

It's really strange, because I couldn't find the reason for a time-difference of 2hours.

It should be: 16:05:20 The code prints: 14:05:20 in both variants.

Could someone please help me and tell me what went wrong here?


Solution 1:

This is the problem:

TimeZone.getTimeZone("Germany")

There's no such time zone ID, so Java in its infinite wisdom decides to just return you UTC without telling you that anything's wrong. Try this instead:

TimeZone.getTimeZone("Europe/Berlin")

Wikipedia has a list of IANA time zone IDs, but it's somewhat out of date (at the time of writing); the IANA data is the most up-to-date, but it's not as easily browsable...

Solution 2:

I believe the problem is the default timezone on the platform you're running on.

java.util.Date() does have a time zone. It maintains "inherited" time zone information, which, it appears, is acquired from the system's default locale.

this code.

TimeZone tz = TimeZone.getTimeZone("GMT-03:00");
Calendar cal = Calendar.getInstance(tz);
cal.set(1953, 2, 22, 4, 20, 13);
Date dateTime = cal.getTime();
System.out.println(dateTime.toString());

yields this on my system, which is uses the PST locale: Sat Mar 21 23:20:13 PST 1953.

I don't believe that there is a way to use the java.util.Date object, or the DateFormat objects which use it, to accurately handle time information from a "foreign" time zone.