views:

355

answers:

4

I have an instance of Java which seems to be using a completely incorrect time zome. Instead of using the Australia/Sydney time zone which Windows is using, it is using the America/Caracas time zone.

I checked the Windows time through the system clock firstly, then checked HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/ and ControlSet001, ControlSet002. All are set to a Sydney time zone.

Does anybody know if this is a bug in Java, or if it is referring to a time set elsewhere?

Java version is 1.6.0_06

+2  A: 

Ensure you set the timezone for the JVM when starting the application:

-Duser.timezone="Australia/Sydney"
Pool
Was hoping for a proper solution, but ended up going with this workaround.
Jonathan Maddison
+1  A: 

Try in your app to get default timezone, or set timezone manually (commented line).

Little example of mine:

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class Main {
    public static void main(String[] args) {
        Locale locale = Locale.getDefault();
        TimeZone localTimeZone = TimeZone.getDefault(); 
        //TimeZone localTimeZone = TimeZone.getTimeZone("Australia/Sydney");
        DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, locale);
        dateFormat.setTimeZone(localTimeZone);
        Date rightNow = new Date();
        System.out.println(locale.toString() + ": " + dateFormat.format(rightNow));
    }
}
Nightsorrow
Thanks guys, I am wondering however _why_ I should have to manually specify the time zone. Shouldn't it be the same as the OS?
Jonathan Maddison
BTW, the above code returns the correct locale, but incorrect time zone.
Jonathan Maddison
It should be. You can try to update Java to the newest version, in case there's a bug.
Nightsorrow
A: 

You should update your JRE/SDK, but TZUpdater may be sufficient.

trashgod
+1  A: 

Check information on the following link: http://techtavern.wordpress.com/2010/04/15/java-and-incorrect-timezone-on-windows-xp/
It shows, that there is a bug in JVM, causing reading incorrect default timezone from windows registry. There is no bug fix yet.

Roman