如何正确设置JVM TimeZone [英] How to set a JVM TimeZone Properly

查看:934
本文介绍了如何正确设置JVM TimeZone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行Java程序,但它采用默认的GMT时区而不是OS定义的时区。我的JDK版本是1.5,操作系统是Windows Server Enterprise(2007)

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指定了一个中心时区,但是当我运行以下程序时,它给了我一个GMT时间。

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());
    }
}

这是输出

sun.util.calendar.ZoneInfo[id="GMT",
offset=0,
dstSavings=0,
useDaylight=false,
transitions=0,
lastRule=null]
Mon Mar 22 13:46:45 GMT 2010

请注意,我不想从应用程序设置时区。我希望JVM使用的时区应该是操作系统中指定的时区。 (我没有发现其他具有1.4版JDK和Microsoft Server 2003的服务器的问题。)

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.

推荐答案

你可以传递JVM这个参数

You can pass the JVM this param

-Duser.timezone

例如

-Duser.timezone=Europe/Sofia

这应该做诀窍。
设置环境变量TZ也可以在Linux上实现。

and this should do the trick. Setting the environment variable TZ also does the trick on Linux.

这篇关于如何正确设置JVM TimeZone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆