如何正确设置 JVM 时区

发布于 2024-08-25 16:53:52 字数 732 浏览 10 评论 0原文

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

Windows 指定了中央时区,但是当我运行以下程序时,它给了我 GMT 时间。

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 的服务器上没有发现此问题)。

任何想法将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

离线来电— 2024-09-01 16:53:52

您可以将此参数传递给 JVM

-Duser.timezone

例如,

-Duser.timezone=Europe/Sofia

,这应该可以解决问题。
在 Linux 上设置环境变量 TZ 也能达到目的。

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.

萌︼了一个春 2024-09-01 16:53:52

您还可以使用以下代码在代码中设置默认时区。

TimeZone.setDefault(TimeZone.getTimeZone("UTC"));

致您的

 TimeZone.setDefault(TimeZone.getTimeZone("Europe/Sofia"));

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"));
沧笙踏歌 2024-09-01 16:53:52

上面接受的答案:

-Duser.timezone="Europe/Sofia" 

并不完全适合我。只有当参数周围没有引号时,我才能成功更改时区:

-Duser.timezone=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
顾铮苏瑾 2024-09-01 16:53:52

在 Windows 7 和 JDK6 上,我必须将 -Duser.timezone="Europe/Sofia" 添加到 JAVA_TOOL_OPTIONS 系统变量位于“我的电脑=>属性=>高级系统设置=>环境变量”下。

如果您已经为 JAVA_TOOL_OPTIONS 设置了一些其他属性,只需附加一个空格,然后插入您的属性字符串。

On windows 7 and for JDK6, I had to add -Duser.timezone="Europe/Sofia" to the JAVA_TOOL_OPTIONS system variable located under "My computer=>Properties=>Advanced System Settings=>Environment Variables".

If you already have some other property set for JAVA_TOOL_OPTIONS just append a space and then insert your property string.

那伤。 2024-09-01 16:53:52

我认为其他答案中没有涵盖两个选项:

避免需要

无论您如何设置 JVM 默认时区,都很难确保没有其他人以不同的方式设置它。它可以随时设置,无需程序的其他部分或同一 JVM 中运行的其他程序通知。因此,在您的时间操作中,请明确您想要的时区,并且您将始终知道您将获得什么,而与 JVM 设置无关。示例:

    System.out.println(ZonedDateTime.now(ZoneId.of("Asia/Dushanbe")));

示例输出:

2018-10-11T14:59:16.742020+05:00[亚洲/杜尚别]

System.setProperty

对于许多用途来说,以下方法不是首选方法,而且肯定会被滥用。对于“扔掉”的程序,我有时发现它很实用。您还可以在 Java 中设置系统属性:

    System.setProperty("user.timezone", "Australia/Tasmania");
    System.out.println(ZonedDateTime.now());

这只是打印出来的:

2018-10-11T21:03:12.218959+11:00[澳大利亚/塔斯马尼亚]

如果您想验证您传递的字符串,请使用:

        System.setProperty("user.timezone", ZoneId.of("Australia/Tasmania").getId());

Two options that I don’t think were covered in the other answers:

Avoid the need

Whatever you do to set the JVM default time zone, it is very hard to make sure that no one else sets it differently. It can be set at any time without notice from another part of your program or from another program running in the same JVM. So in your time operations be explicit about which time zone you want, and you will always know what you get independently of the JVM setting. Example:

    System.out.println(ZonedDateTime.now(ZoneId.of("Asia/Dushanbe")));

Example output:

2018-10-11T14:59:16.742020+05:00[Asia/Dushanbe]

System.setProperty

For many purposes the following will not be the preferred way, and it can certainly be misused. For “throw away” programs I sometimes find it practical. You can also set a system property from within Java:

    System.setProperty("user.timezone", "Australia/Tasmania");
    System.out.println(ZonedDateTime.now());

This just printed:

2018-10-11T21:03:12.218959+11:00[Australia/Tasmania]

If you want validation of the string you are passing, use:

        System.setProperty("user.timezone", ZoneId.of("Australia/Tasmania").getId());
少跟Wǒ拽 2024-09-01 16:53:52

如果您使用 Maven :

mvn -Dexec.args="-Duser.timezone=Europe/Sofia ....."

If you are using Maven:

mvn -Dexec.args="-Duser.timezone=Europe/Sofia ....."
猛虎独行 2024-09-01 16:53:52

设置环境变量 TZ 也应该有效,

例如:export TZ=Asia/Shanghai

Setting environment variable TZ should also works

ex: export TZ=Asia/Shanghai

咋地 2024-09-01 16:53:52

在win7中,如果要在JRE中设置正确的时区作为参数,则必须编辑存储在路径c:\users\%username%\appdata\中的文件deployment.properties locallow\sun\java\deployment 添加字符串 deployment.javaws.jre.1.args=-Duser.timezone\=my_time_zone

In win7, if you want to set the correct timezone as a parameter in JRE, you have to edit the file deployment.properties stored in path c:\users\%username%\appdata\locallow\sun\java\deployment adding the string deployment.javaws.jre.1.args=-Duser.timezone\=my_time_zone

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文