简体   繁体   English

如何获取Java服务器的时区?

[英]How to get Java server's time zone?

I am using Java 7 on a Windows machine. 我在Windows计算机上使用Java 7。 I am developing a web app running on Tomcat. 我正在开发在Tomcat上运行的Web应用程序。

I need to know the server's time zone. 我需要知道服务器的时区。 Here is what I do for this purpose: 为此,我要做的是:

TimeZone.getDefault(); TimeZone.getDefault();

Is this the correct way? 这是正确的方法吗?

Thanks for your input! 感谢您的输入!

Regards. 问候。

tl;dr tl; dr

TimeZone.getDefault()

Is this the correct way? 这是正确的方法吗?

No, that class is now legacy, replaced by ZoneId . 不,该类现在是旧版,由ZoneId代替。

ZoneId.systemDefault()

But better if you specify your desired/expected zone rather than rely on JVM's current default. 但是,如果您指定所需/期望区域而不是依赖JVM当前的默认值,那就更好了。

java.time java.time

The modern solution uses the java.time classes ( ZoneId , ZoneOffset , and ZoneRules ) rather than the troublesome old legacy date-time classes ( TimeZone ). 现代的解决方案使用java.time类( ZoneIdZoneOffsetZoneRules ),而不是麻烦的旧旧式日期时间类( TimeZone )。

Current default 当前默认

You can ask for the JVM's current default time zone. 您可以询问JVM的当前默认时区。

ZoneId z = ZoneId.systemDefault() ;

But whatever purpose you have in mind is likely ill-advised. 但是,无论您想到什么目的,都是不明智的选择。 Firstly, best practice for servers is to set the host OS and the JVM's default time zone to UTC. 首先,服务器的最佳实践是将主机操作系统和JVM的默认时区设置为UTC。 Secondly, you should never depend on the server's time zone. 其次,您永远不应依赖服务器的时区。 That setting is out of your control as a programmer, and easily changed to an unexpected value. 该设置是程序员无法控制的,并且很容易更改为意外的值。 Within a JVM, any code in any thread of any app within that JVM may change the current default time zone during runtime . 在JVM中,该JVM中任何应用程序的任何线程中的任何代码都可以在运行时更改当前的默认时区。

Specify zone 指定区域

Better to always specify your desired/expected time zone by passing an optional argument to the various java.time methods. 最好始终通过将可选参数传递给各种java.time方法来指定所需/期望的时区。

ZonedDateTime zdt = ZonedDateTime.now( ZoneId.of( "Africa/Tunis" ) ) ;

Specify a proper time zone name in the format of continent/region , such as America/Montreal , Africa/Casablanca , or Pacific/Auckland . continent/region的格式指定正确的时区名称 ,例如America/MontrealAfrica/CasablancaPacific/Auckland Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!). 切勿使用ESTIST等3-4个字母的缩写,因为它们不是真实的时区,不是标准化的,甚至不是唯一的(!)。

ZoneId z = ZoneId.of( "America/Montreal" ) ;  

By the way, ditto for Locale — always specify explicitly rather than rely implicitly on current default. 顺便说一句,“ Locale同上-始终明确指定而不是隐式依赖当前默认值。


About java.time 关于java.time

The java.time framework is built into Java 8 and later. java.time框架内置于Java 8及更高版本中。 These classes supplant the troublesome old legacy date-time classes such as java.util.Date , Calendar , & SimpleDateFormat . 这些类取代了麻烦的旧的旧式日期时间类,例如java.util.DateCalendarSimpleDateFormat

The Joda-Time project, now in maintenance mode , advises migration to the java.time classes. 现在处于维护模式Joda-Time项目建议迁移到java.time类。

To learn more, see the Oracle Tutorial . 要了解更多信息,请参见Oracle教程 And search Stack Overflow for many examples and explanations. 并在Stack Overflow中搜索许多示例和说明。 Specification is JSR 310 . 规格为JSR 310

You may exchange java.time objects directly with your database. 您可以直接与数据库交换java.time对象。 Use a JDBC driver compliant with JDBC 4.2 or later. 使用与JDBC 4.2或更高版本兼容的JDBC驱动程序 No need for strings, no need for java.sql.* classes. 不需要字符串,不需要java.sql.*类。

Where to obtain the java.time classes? 在哪里获取java.time类?

The ThreeTen-Extra project extends java.time with additional classes. ThreeTen-Extra项目使用其他类扩展了java.time。 This project is a proving ground for possible future additions to java.time. 该项目为将来可能在java.time中添加内容提供了一个试验场。 You may find some useful classes here such as Interval , YearWeek , YearQuarter , and more . 您可以在这里找到一些有用的类,比如IntervalYearWeekYearQuarter ,和更多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM