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

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

问题描述

我正在尝试运行 Java 程序,但它采用的是默认的 GMT 时区,而不是操作系统定义的时区.我的 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).

任何想法将不胜感激.

推荐答案

你可以给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 时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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