如何改变Tomcat 7的服务器时区? [英] How to change Tomcat 7's server timezone?

查看:132
本文介绍了如何改变Tomcat 7的服务器时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序部署在美国洛杉矶的Debian vps中。所以代码如 new SimpleDateFormat(yyyy-MM-dd HH:mm)。format(new Date())将返回America / Los Angeles的当前时间。

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.

我可以在Tomcat的配置文件中进行一些设置( server.xml catalina.sh 或者什么?)这样获取当前时间将返回指定的TimeZone,如 GMT + 8 亚洲/台北 ???

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+8 or Asia/Taipei ???

推荐答案

在所有可以设置时区的地方,(通常)总是最好在你处理时明确设置时区倍。是的,您的服务器位于洛杉矶,但您的用户在哪里?

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?

因为显式处理时区会使你的应用程序变得更复杂(但也更正确,更不令人惊讶,更难测试),下一个最好的方法就是明确地制作tomcat(java) )知道服务器时钟设置的时区。小心:有一些级别可以设置:将服务器时钟设置为UTC,将服务器操作系统配置为PST,然后让java知道服务器所在的时区,例如:在 setenv.sh CATALINA_OPTS =$ CATALINA_OPTS -Duser.timezone = America / Los_Angeles(或任何你的时区is)为您的时区配置Java。

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.

测试,冲洗,重复直至满意配置。但是,在所有不同级别上,您可以将其设置为明确的选择。解析时区相当于一个java而不是一个tomcat特性。

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.

对于始终存储时间而言,软件的可维护性非常重要。如果您存储在当地时区,计算任何其他时区将是一团糟 - 想想夏令时,世界不同地区的时区变化等。

It's quite important for maintainability of your software to always store 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,然后获取当前时间,检查它是否正确。出于显示目的,您可以使用(用户)本地时区(例如PST),但是对于存储和计算,强烈建议使用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.

这篇关于如何改变Tomcat 7的服务器时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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