简体   繁体   中英

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. On UI, I am showing a list of timezones by calling - TimeZone.getAvailableIDs(); This retrieves TZ info from the " tzdb.dat " which comes along with the JRE.

But some of the timezones like "IST" are not supported by the native system. And these kind of unsupported Timezones are returned by TimeZone.getAvailableIDs().

So is there any way in Java where we can retrieve native system supported timezones ?

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. See examples on this Wikipedia page . And learn about tzdata maintained currently by IANA.

You asked:

is there any way in Java where we can retrieve native system supported timezones ?

The TimeZone class in Java is now obsolete, supplanted by the modern java.time class ZoneId . 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. Be sure to keep the tzdata in your Java implementation up-to-date. Ditto for your host OS. And your database system as well, with some such as Postgres having their own internal copy.

True time zones

You said:

But some of the timezones like "IST"

IST is not a real time zone. 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 . 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(!).

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. Indeed, servers should usually be set for UTC (an offset of zero hours-minutes-seconds).

In your Java code, always specify your desired/expected time zone. 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.

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). 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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