Java 如何正确设置 JVM 时区
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2493749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set a JVM TimeZone Properly
提问by Kushal Paudyal
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)
我正在尝试运行 Java 程序,但它采用默认的 GMT 时区而不是操作系统定义的时区。我的 JDK 版本是 1.5,操作系统是 Windows Server Enterprise (2007)
Windows has a Central timezone specified, but when I run the following program, it gives me a GMT time.
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());
}
}
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).
请注意,我不想从应用程序设置时区。我希望 JVM 使用的时区应该是操作系统中指定的时区。(我没有发现其他具有 1.4 版 JDK 和 Microsoft Server 2003 的服务器存在此问题)。
Any thoughts would be highly appreciated.
任何想法将不胜感激。
采纳答案by Bozhidar Batsov
You can pass the JVM this param
您可以将这个参数传递给 JVM
-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.
这应该可以解决问题。设置环境变量 TZ 在 Linux 上也有效果。
回答by WN4yhihiY
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
回答by Dave
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
在win7中,如果要在JRE中设置正确的时区作为参数,则必须编辑deployment.properties
路径中存储的文件c:\users\%username%\appdata\locallow\sun\java\deployment
并添加字符串deployment.javaws.jre.1.args=-Duser.timezone\=
my_time_zone
回答by VolkanT
On windows 7 and for JDK6, I had to add -Duser.timezone="Europe/Sofia"
to the JAVA_TOOL_OPTIONSsystem variable located under "My computer=>Properties=>Advanced System Settings=>Environment Variables".
在 Windows 7 和 JDK6 上,我必须添加-Duser.timezone="Europe/Sofia"
到位于“我的电脑=>属性=>高级系统设置=>环境变量”下的JAVA_TOOL_OPTIONS系统变量。
If you already have some other property set for JAVA_TOOL_OPTIONS
just append a space and then insert your property string.
如果您已经设置了一些其他属性,JAVA_TOOL_OPTIONS
只需附加一个空格,然后插入您的属性字符串。
回答by MichalSv
回答by Ali786
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"));
回答by Ole V.V.
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:
无论您如何设置 JVM 默认时区,都很难确保没有其他人以不同方式设置它。它可以随时设置,无需从程序的另一部分或在同一 JVM 中运行的另一个程序通知。因此,在您的时间操作中明确您想要的时区,并且您将始终知道独立于 JVM 设置的内容。例子:
System.out.println(ZonedDateTime.now(ZoneId.of("Asia/Dushanbe")));
Example output:
示例输出:
2018-10-11T14:59:16.742020+05:00[Asia/Dushanbe]
2018-10-11T14:59:16.742020+05:00[亚洲/杜尚别]
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:
出于许多目的,以下方法不是首选方法,并且肯定会被滥用。对于“扔掉”程序,我有时觉得它很实用。您还可以从 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]
2018-10-11T21:03:12.218959+11:00[澳大利亚/塔斯马尼亚]
If you want validation of the string you are passing, use:
如果要验证传递的字符串,请使用:
System.setProperty("user.timezone", ZoneId.of("Australia/Tasmania").getId());