Strange Java Timezone Date Conversion Problem

前端 未结 3 785
北荒
北荒 2020-12-04 01:28

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

3条回答
  •  悲哀的现实
    2020-12-04 02:02

    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.

提交回复
热议问题