I’m trying to get the short name for the system default timezone using
java.time.ZoneId. However, the API is returning an unexpected value. For example, when the server is in
Asia/Kolkata, I am getting
IT instead of
IST. The same thing is happening when
PT is returned instead of
PST. Here is my code snippet:
//String timeZone = ZoneId.of("Asia/Kolkata").getDisplayName(TextStyle.SHORT, Locale.ENGLISH);
String timeZone = ZoneId.systemDefault().getDisplayName(TextStyle.SHORT, Locale.ENGLISH);
I need to get the well known short name for the timezone. Can anyone help me with this?
The issue you are facing is due to the fact that
ZoneId.systemDefault() returns the system default timezone, which can sometimes be in the format of a three-letter abbreviation that is not well-known or standardized.
To get the well-known short name for the timezone, you can use the
java.util.TimeZone class instead. Here is an updated code snippet that should give you the expected results:
String timeZone = TimeZone.getDefault().getDisplayName(false, TimeZone.SHORT, Locale.ENGLISH);
This will give you the short name for the system default timezone in a well-known and standardized format, such as