简体   繁体   English

使用 Java 更改系统的时区

[英]Changing timezone of a system using Java

I am currently writing an application where I want to provide a feature wherein a user can change the current time and timezone of the system.我目前正在编写一个应用程序,我想提供一个功能,用户可以在其中更改系统的当前时间和时区。 Using the input timezone value , I run a shell script which links the local time to the passed timezone.使用输入时区值,我运行一个 shell 脚本,它将本地时间链接到传递的时区。 On UI, I am showing a list of timezones by calling - TimeZone.getAvailableIDs();在 UI 上,我通过调用显示时区列表 - TimeZone.getAvailableIDs(); This retrieves TZ info from the " tzdb.dat " which comes along with the JRE.这将从 JRE 附带的“ tzdb.dat ”中检索 TZ 信息。

But some of the timezones like "IST" are not supported by the native system.但是本机系统不支持某些时区,例如“IST”。 And these kind of unsupported Timezones are returned by TimeZone.getAvailableIDs().这些不受支持的时区由TimeZone.getAvailableIDs().返回TimeZone.getAvailableIDs().

So is there any way in Java where we can retrieve native system supported timezones ?那么在 Java 中有什么方法可以检索本机系统支持的时区吗?

Time zones have a poor history时区的历史很糟糕

Time zones are, surprisingly, a real mess.令人惊讶的是,时区真是一团糟。 I have been surprised to learn that they have not historically been well-defined and standardized.我很惊讶地发现它们在历史上并没有明确定义和标准化。

In more recent times, a format of Continent/Region seems to have been widely adopted.最近,一种Continent/Region格式似乎已被广泛采用。 See examples on this Wikipedia page .请参阅此 Wikipedia 页面上的示例。 And learn about tzdata maintained currently by IANA.并了解 IANA 当前维护的tzdata

You asked:你问:

is there any way in Java where we can retrieve native system supported timezones ?在 Java 中有什么方法可以检索本机系统支持的时区吗?

The TimeZone class in Java is now obsolete, supplanted by the modern java.time class ZoneId . Java 中的TimeZone类现已过时,取而代之的是现代java.timeZoneId Get a set of time zone names. 获取一组时区名称。

Set< String > zoneIds = ZoneId.getAvailableZoneIds() ;

Time zones change surprisingly often, both in their name and their currently used offset-from-UTC.时区经常发生令人惊讶的变化,无论是名称还是当前使用的 UTC 偏移量。 Be sure to keep the tzdata in your Java implementation up-to-date.请务必使 Java 实现中的tzdata保持最新。 Ditto for your host OS.您的主机操作系统也是如此。 And your database system as well, with some such as Postgres having their own internal copy.还有你的数据库系统,比如Postgres有自己的内部副本。

True time zones真实时区

You said:你说:

But some of the timezones like "IST"但一些时区,如“IST”

IST is not a real time zone. IST不是实时时区。 It is a pseudo-zone, which could mean Irish Standard Time, India Standard Time, or something else.这是一个伪区域,可能意味着爱尔兰标准时间、印度标准时间或其他时间。

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 2-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).永远不要使用ESTIST等 2-4 个字母的缩写,因为它们不是真正的时区,不是标准化的,甚至不是唯一的(!)。

If you meant India time , use:如果您指的是印度时间,请使用:

ZoneId z = ZoneId.of( "Asia/Kolkata" ) ;

If you meant Ireland time , use:如果您指的是爱尔兰时间,请使用:

ZoneId z = ZoneId.of( "Europe/Dublin" ) ;

Do not rely on current default time zone不要依赖当前的默认时区

As commented by Matt Johnson-Pint , you should generally not be altering your host OS' default time zone.正如Matt Johnson-Pint评论的那样,您通常不应更改主机操作系统的默认时区。 Indeed, servers should usually be set for UTC (an offset of zero hours-minutes-seconds).实际上,服务器通常应该设置为 UTC(零时分秒的偏移量)。

In your Java code, always specify your desired/expected time zone.在您的 Java 代码中,始终指定您想要/期望的时区。 The JVM's current default time zone can be altered at any moment by any code in any thread of any app within the JVM — so depending on the current default is not reliable. JVM 的当前默认时区可以随时被 JVM 内任何应用程序的任何线程中的任何代码更改——因此依赖当前默认值是不可靠的。

If you want the current moment as seen in India, specify the appropriate time zone.如果您想要在印度看到的当前时刻,请指定适当的时区。

ZoneId z = ZoneId.of( "Asia/Kolkata" ) ;
ZonedDateTime zdt = ZonedDateTime.now( z ) ;  // Capture the current moment as seen in the wall-clock time used by the people of a particular region (a time zone). 

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

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