Pseudo time zone names
These 2-4 letter "short time zone" names are not actually real time zone names. They are not standardized. They are not even unique!
Examples:
- "CST" = "Central Standard Time" (Americas) and "China Standard Time"
- "IST" = "India Standard Time" and "Ireland Standard Time"
Since there is no official definition, I do not see how you would expect a localized version.
Stick with using real time zone names in Continent/Region
format such as Africa/Tunis
, America/Chicago
, and Pacific/Auckland
.
java.time classes: ZoneId
& ZoneOffset
The TimeZone
class is part of the terrible date-time classes bundled with the earliest versions of Java, such as Date
& Calendar
. These were supplanted years ago by the modern java.time classes defined in JSR 310.
The TimeZone
class was replaced by ZoneId
and ZoneOffset
classes.
TextStyle
with localized ZoneId#getDisplayName
The ZoneId
class offers a getDisplayName
method. That method takes an argument for TextStyle
, to determine the length or abbreviation.
Let's write code to use each of the six text styles.
System.out.println( "Java version: " + Runtime.version() );
System.out.println(
"tz database version: " +
java.time.zone.ZoneRulesProvider
.getVersions( "UTC" )
.lastEntry()
.getKey()
+ "\n"
);
ZoneId zoneId = ZoneId.of( "America/Chicago" );
Locale locale = Locale.US; // new Locale( "zh" , "CN" );
for ( TextStyle textStyle : TextStyle.values() ) {
String output = zoneId.getDisplayName( textStyle , locale );
System.out.println( "output for " + textStyle + " = " + output );
}
When run.
Java version: 17+35-2724
tz database version: 2021a
output for FULL = 北美中部时间
output for FULL_STANDALONE = 北美中部时间
output for SHORT = CT
output for SHORT_STANDALONE = CT
output for NARROW = America/Chicago
output for NARROW_STANDALONE = CT
When run for Locale.US
.
Java version: 17+35-2724
tz database version: 2021a
output for FULL = Central Time
output for FULL_STANDALONE = Central Time
output for SHORT = CT
output for SHORT_STANDALONE = CT
output for NARROW = America/Chicago
output for NARROW_STANDALONE = CT
FormatStyle
with DateTimeFormatter.ofLocalizedDateTime
And let's try some code using the time zones in action with a ZonedDateTime
object.
Here we use a DateTimeFormatter
from ofLocalizedDateTime
to automatically localize our generated text. This class takes an argument of FormatStyle
(not to be confused with the TextStyle
we saw above) to determine length versus abbreviation. So we loop all four enum objects of FormatStyle
: FULL
, LONG
, MEDIUM
, & SHORT
.
Locale locale = new Locale( "zh" , "CN" );
ZonedDateTime zonedDateTime = ZonedDateTime.now( ZoneId.of( "America/Chicago" ) );
for ( FormatStyle formatStyle : FormatStyle.values() ) {
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime( formatStyle ).withLocale( locale );
String output = zonedDateTime.format( formatter );
System.out.println( "output for FormatStyle " + formatStyle + " = " + output );
}
When run.
output for FormatStyle FULL = 2021年8月9日星期一 北美中部夏令时间 上午12:19:42
output for FormatStyle LONG = 2021年8月9日 CDT 上午12:19:42
output for FormatStyle MEDIUM = 2021年8月9日 上午12:19:42
output for FormatStyle SHORT = 2021/8/9 上午12:19
And when run for Locale.US
.
output for FormatStyle FULL = Monday, August 9, 2021 at 12:20:40 AM Central Daylight Time
output for FormatStyle LONG = August 9, 2021 at 12:20:40 AM CDT
output for FormatStyle MEDIUM = Aug 9, 2021, 12:20:40 AM
output for FormatStyle SHORT = 8/9/21, 12:20 AM
We can see that, for both locales, the LONG
format style uses CDT
as the unofficial short name of the time zone.
Lastly, again, I recommend you avoid using these 2-4 letter pseudo zone names wherever practical. And when exchanging date-time data as text, never localize; always use ISO 8601 formats for data-exchange.
America/Chicago
?TimeZone
. That class is poorly designed and long outdated. Instead useZoneId
from java.time, the modern Java date and time API.