How to set a JVM TimeZone Properly
I am trying to run a Java program, but it is taking a default GMT timezone instead of an OS defined timezone. My JDK version is 1.5 and the OS is Windows Server Enterprise (2007)
Windows has a Central timezone specified, but when I run the following program, it gives me a GMT time.
import java.util.Calendar;
public class DateTest
{
public static void main(String[] args)
{
Calendar now = Calendar.getInstance();
System.out.println(now.getTimeZone());
System.out.println(now.getTime());
}
}
Here is the output
sun.util.calendar.ZoneInfo[id="GMT",
offset=0,
dstSavings=0,
useDaylight=false,
transitions=0,
lastRule=null]
Mon Mar 22 13:46:45 GMT 2010
Please note that I do not want to set the timezone from the application. I want that the timezone used by JVM should be the one specified in the OS. (I am not finding this issues with other servers that have version 1.4 of JDK and Microsoft Server 2003).
Any thoughts would be highly appreciated.
You can pass the JVM this param
-Duser.timezone
For example
-Duser.timezone=Europe/Sofia
and this should do the trick. Setting the environment variable TZ also does the trick on Linux.
You can also set the default time zone in your code by using following code.
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
To Yours
TimeZone.setDefault(TimeZone.getTimeZone("Europe/Sofia"));
The accepted answer above:
-Duser.timezone="Europe/Sofia"
Didn't work for me exactly. I only was able to successfully change my timezone when I didn't have quotes around the parameters:
-Duser.timezone=Europe/Sofia
链接地址: http://www.djcxy.com/p/18760.html
上一篇: 启动JVM时,Xms和Xmx参数是什么?
下一篇: 如何正确设置JVM时区