Normally, using z
would give you the correct timezone, here with HongKong :
System.out.println(
new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy z", Locale.ENGLISH)
.parse("Tue May 07 15:41:50 2019 HKT")
);
Tue May 07 09:41:50 CEST 2019
Documentation
But like it is explained in TimeZone
, a "Three-letter time zone IDs" is not unique and this is the reason it should not be used.
[...] some other three-letter time zone IDs (such as "PST", "CTT", "AST") are also supported. However, their use is deprecated because the same abbreviation is often used for multiple time zones (for example, "CST" could be U.S. "Central Standard Time" and "China Standard Time"), and the Java platform can then only recognize one of them.
Proof of concept
We can check that IST
represents more than one timezone with this dirty code to get every Timezone
matching the ZoneId of every "IST" zone supported by the SimpleDateFormatter
:
String shortZoneID = "IST";
Arrays.stream(
new SimpleDateFormat().getDateFormatSymbols().getZoneStrings())
.filter(s -> shortZoneID.equals(s[2]) || shortZoneID.equals(s[4]))
.map(s -> TimeZone.getTimeZone(s[0]))
.map(t -> String.format("%-50s %s", t.getDisplayName(), Duration.of(t.getRawOffset(), ChronoUnit.MILLIS)))
.distinct()
.forEach(System.out::println)
;
Output :
Israel Standard Time PT2H
India Standard Time PT5H30M
Greenwich Mean Time PT0S
Explaining why IST" seems to be in GMT.
System.out.println(
new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy z", Locale.ENGLISH)
.parse("Tue May 07 15:41:50 2019 IST")
);
Tue May 07 15:41:50 CEST 2019
Not sure yet how Java prioritize the timezone in this case, does it take the first found, or is it based on the current locale (I am on GMT+01:00 timezone)?
But even with this information, the result might not be correct.
Solutions
In the meantime, here are some correct form of recovering a Timezone :
Using the offset
System.out.println(
new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy XXX", Locale.ENGLISH)
.parse("Tue May 07 15:41:50 2019 +05:30"));
Using the full name of the zone
System.out.println(
new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy zzzz", Locale.ENGLISH)
.parse("Tue May 07 15:41:50 2019 India Standard Time"));
Both output :
Tue May 07 12:11:50 CEST 2019