如何正确设置 JVM 时区
我正在尝试运行 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可以将此参数传递给 JVM
例如,
,这应该可以解决问题。
在 Linux 上设置环境变量 TZ 也能达到目的。
You can pass the JVM this param
For example
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.
To Yours
上面接受的答案:
并不完全适合我。只有当参数周围没有引号时,我才能成功更改时区:
The accepted answer above:
Didn't work for me exactly. I only was able to successfully change my timezone when I didn't have quotes around the parameters:
在 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.我认为其他答案中没有涵盖两个选项:
避免需要
无论您如何设置 JVM 默认时区,都很难确保没有其他人以不同的方式设置它。它可以随时设置,无需程序的其他部分或同一 JVM 中运行的其他程序通知。因此,在您的时间操作中,请明确您想要的时区,并且您将始终知道您将获得什么,而与 JVM 设置无关。示例:
示例输出:
System.setProperty
对于许多用途来说,以下方法不是首选方法,而且肯定会被滥用。对于“扔掉”的程序,我有时发现它很实用。您还可以在 Java 中设置系统属性:
这只是打印出来的:
如果您想验证您传递的字符串,请使用:
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:
Example output:
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:
This just printed:
If you want validation of the string you are passing, use:
如果您使用 Maven :
If you are using Maven:
设置环境变量
TZ
也应该有效,例如:
export TZ=Asia/Shanghai
Setting environment variable
TZ
should also worksex:
export TZ=Asia/Shanghai
在win7中,如果要在JRE中设置正确的时区作为参数,则必须编辑存储在路径
c:\users\%username%\appdata\中的文件
添加字符串deployment.properties
locallow\sun\java\deploymentdeployment.javaws.jre.1.args=-Duser.timezone\=
my_time_zoneIn win7, if you want to set the correct timezone as a parameter in JRE, you have to edit the file
deployment.properties
stored in pathc:\users\%username%\appdata\locallow\sun\java\deployment
adding the stringdeployment.javaws.jre.1.args=-Duser.timezone\=
my_time_zone