1

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 ?

Aldeguer
  • 821
  • 9
  • 32
Rahul Vedpathak
  • 1,346
  • 3
  • 16
  • 30
  • No, there is no Java generic way to access the OS's time zone database. – Andreas Sep 27 '17 at 12:38
  • 1
    The best alternative is to just filter the results returned by `TimeZone.getAvailableIDs()` so they match your system's supported zones. But if you're using Java 8, why not use the new API's `java.time.ZoneId` class, and call `ZoneId.getAvailableZoneIds()`? You'll have to filter the results anyway, but at least this doesn't return **most** of those short names (I've tested in JDK 1.8.0_144 and it doesn't return "IST") - some are still returned due to retro-compatibility reasons, though. –  Sep 27 '17 at 12:45
  • Java is not good for interacting with the underlying platform. Any reason you chose this language for your application? – Thorbjørn Ravn Andersen Sep 27 '17 at 12:46
  • 2
    Not directly related to your problem, but anyway: short names such as `IST` or `EST` are [ambiguous and not standard](https://stackoverflow.com/a/18407231/7605325). **IST**, for example, can be *India Standard Time*, *Israel Standard Time* or *Irish Standard Time*. Some systems simply don't support such names due to this ambiguity, while others (like `java.util.TimeZone`) just assume some arbitrary default for them. –  Sep 27 '17 at 12:48
  • 1
    In general, *applications* should not manage the system-wide time zone. Let the operating system do that. Can you imagine if multiple applications were running that wanted *different* system-wide time zone settings? Nothing good can come of that. – Matt Johnson-Pint Sep 27 '17 at 20:40

1 Answers1

2

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). 
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154