Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 384
Thomas Pototschnig Avatar asked Aug 12 '11 14:08

Thomas Pototschnig


3 Answers

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...

like image 77
Jon Skeet Avatar answered Oct 14 '22 13:10

Jon Skeet


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.

like image 25
sherman Avatar answered Oct 14 '22 14:10

sherman


The Answer by Jon Skeet is correct, you used an incorrect time zone name.

java.time

Here is a solution using the modern java.time classes that supplant the old legacy date-time classes that have proven to be so troublesome and confusing.

Instant instant = Instant.ofEpochMilli( milliseconds_since_1970 );  // Or Instant.now() for current moment.
ZoneId z = ZoneId.of( "Europe/Berlin" ); 
ZonedDateTime zdt = instant.atZone( z );

Generate a localized string to represent that date-time value.

Locale l = Locale.GERMANY; // Or Locale.CANADA_FRENCH, etc.
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.SHORT ).withLocale( l );
String output = zdt.format( f );

Table of date-time types in Java, both modern and legacy

like image 1
Basil Bourque Avatar answered Oct 14 '22 13:10

Basil Bourque