tonix
tonix

Reputation: 6939

Why Java is displaying a wrong time (1 hour ahead) even if I have set the timezone?

Hi everyone I have some troubles with the Date object in Java, actually if I instantiate an empty Date instance:

Date date = new Date();
System.out.println("Date = " + date);

Its value is 23:30 while I am in Moscow and the time here is 22:30.

If I try to use a custom format and set the timezone with a SimpleDataFormat object, like this:

Date date = (Date) prop.getValue();
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy, HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("Europe/Moscow"));
System.out.println("Date = " + sdf.format(date));

I also get an overhead of 1 hour Date = 23:35 while the correct time where I am is 22:30.

Also the problem is that the default TimeZone of my JVM is set correctly:

System.out.println("TIMEZONE : " + TimeZone.getDefault().getID());

Actaully outputs TIMEZONE : Europe/Moscow

So actually, where is the problem?

EDIT: I have found a solution, just look at my answer below.

Upvotes: 0

Views: 1158

Answers (1)

tonix
tonix

Reputation: 6939

I have found how to resolve this issue.

1) Go to http://www.oracle.com/technetwork/java/javase/downloads/tzupdater-download-513681.html

2) Download the .zip, unzip it, close all java applications currently running, go to the tzupdater-1.4.9-2014i folder

3) Open a command line shell and type:

$ java -jar tzupdater.jar -u

Root privilege might be required.

After that, restart the Java applications, and now:

 System.out.println(new GregorianCalendar().getTime());
 System.out.println(new java.util.Date());

Both will output Sun Dec 14 00:02:59 MSK 2014, which is the correct current time in Moscow, no extra hour!

Upvotes: 2

Related Questions