Java 如何更改 Tomcat 7 的服务器时区?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28777322/
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 change Tomcat 7's server timezone?
提问by Sam Su
My application deployed in a Debian vps in US, Los Angeles. So code like new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date())
will return current time of America/Los Angeles.
我的应用程序部署在美国洛杉矶的 Debian vps 中。所以像这样的代码new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date())
将返回美国/洛杉矶的当前时间。
Can I do some setting in Tomcat's configuration file (server.xml
or catalina.sh
or what?) so that get current time will return a specified TimeZone like GMT+8or Asia/Taipei???
我可以在 Tomcat 的配置文件(server.xml
或catalina.sh
什么?)中进行一些设置,以便获取当前时间将返回指定的时区,如GMT+8或Asia/Taipei???
采纳答案by Olaf Kock
With all of the different places where you can set timezones, it's (in general) always best to explicitly set the timezone when you're dealing with times. Yes, your server is in Los Angeles, but where are your users?
对于可以设置时区的所有不同地方,(通常)最好在处理时间时明确设置时区。是的,您的服务器在洛杉矶,但您的用户在哪里?
As explicitly dealing with timezones makes your application somewhat more complex (but also more correct, less surprising, harder to test) the next best would be to explicitly make tomcat (java) know what timezone your server clock is set to. Careful: There are some levels to set this: Set your server clock to UTC, configure your server OS to be PST and then let java know of the timezone that your server is on, e.g. in setenv.sh
do CATALINA_OPTS="$CATALINA_OPTS -Duser.timezone=America/Los_Angeles"
(or whatever your timezone is) to configure Java for your timezone.
由于显式处理时区会使您的应用程序更复杂(但也更正确、更不令人惊讶、更难测试),因此下一个最好的方法是显式让 tomcat (java) 知道您的服务器时钟设置为哪个时区。小心:有一些级别可以设置:将您的服务器时钟设置为 UTC,将您的服务器操作系统配置为 PST,然后让 java 知道您的服务器所在的时区,例如在setenv.sh
做CATALINA_OPTS="$CATALINA_OPTS -Duser.timezone=America/Los_Angeles"
(或任何您的时区)进行配置Java 为您的时区。
Test, rinse, repeat until happy with the configuration. But make it an explicit choice on all different levels that you can set your hands on. Resolving the timezone is rather a java than a tomcat feature.
测试、冲洗、重复直到对配置满意为止。但是,请在您可以着手的所有不同级别上做出明确的选择。解析时区与其说是一个 tomcat 功能,不如说是一个 Java 功能。
It's quite important for maintainability of your software to alwaysstore times in UTC. If you ever store in your local timezone, calculating any other timezone will be a mess - think daylight savings times, change of timezones of different areas of the world etc.
对于软件的可维护性而言,始终以 UTC 存储时间非常重要。如果您曾经存储在本地时区,计算任何其他时区将是一团糟 - 想想夏令时,世界不同地区的时区变化等。
So: Set your server to UTC, then get the current time, check if it's correct. For display purposes, you might use the (user's) local timezone (e.g. PST), but for storage and calculation, UTC is highly recommended.
所以:将您的服务器设置为UTC,然后获取当前时间,检查它是否正确。出于显示目的,您可以使用(用户的)本地时区(例如 PST),但为了存储和计算,强烈建议使用 UTC。
回答by M S Parmar
Tomcat's personal timezone would be specified in its startup script in a form like:
Tomcat 的个人时区将在其启动脚本中以如下形式指定:
-Duser.timezone=GMT
回答by Aparajita
For Windows - Go to catalina.bat and add CATALINA_OPTS property(under start).
对于 Windows - 转到 catalina.bat 并添加 CATALINA_OPTS 属性(在开始下)。
:doStart shift set CATALINA_OPTS=-Duser.timezone=America/Denver
:doStart shift set CATALINA_OPTS=-Duser.timezone=America/Denver
回答by Purushothaman
If you want to change it from eclipse... Run --> Run configuration --> Apache tomcat --> Tomcat Server --> VM arguments add -Duser.timezone=America/Montreal
如果你想从 eclipse... 运行 --> 运行配置 --> Apache tomcat --> Tomcat 服务器 --> VM 参数添加 -Duser.timezone=America/Montreal
回答by Root
In the Linux just add the following line in setenv.sh
which is at CATALINA_HOME/bin/
.
CATALINA_OPTS="-Duser.timezone=Asia/{your zone}"
在 Linux 中,只需添加以下行,setenv.sh
其中位于CATALINA_HOME/bin/
.
CATALINA_OPTS="-Duser.timezone=Asia/{your zone}"