java: how to get Timezone abbreviation (from offse

2019-07-16 13:37发布

问题:

My code like belows, I am using android. I found different devices may have different result.

Using different phone: I can get : "EST" or "GMT-05:00".

However, I just want to get abbreviation(just like "EST").

How can I get the abbreviation (or change offset to abbreviation)?

   String timezone =Calendar.getInstance().getTimeZone().getDisplayName(false, TimeZone.SHORT);

回答1:

If anyone else need the solution : I just tweak the code and get desired short name, which i needed to display to user.

private static String getTimeZoneShortName() {

    String tz = TimeZone.getDefault().getDisplayName();

    String[] stz = tz.split(" ");

    StringBuilder sName = new StringBuilder();

    for (int i = 0; i < stz.length; i++) {
        sName.append(stz[i].charAt(0));
    }
    return sName.toString();
}