11

I would like to show the "name" of timezone in my android program. Say if it is "GMT+8:00", then show "HongKong"; By searching, i find that the getDisplayName function should sever my purpose.

http://developer.android.com/reference/java/util/TimeZone.html#getDisplayName(boolean, int, java.util.Locale)

However, in my own program, this function only shows "GMT+8:00" but when i use "getDisplayName" in the Google Open Source Project, it would show the name "HongKong" instead.

Does anyone know the reason behind this?

leppie
  • 115,091
  • 17
  • 196
  • 297
Antoine Murion
  • 773
  • 1
  • 14
  • 26
  • I would guess it's because timezones go by many names, including just the offset. UTC+8 is actually the most populated timezone in the world, so it has a particularly large number of names, WST, IRKT, Australia/Perth and Asia/Singapore. You'll have to find a subclass of `TimeZone` (and probably of `SimpleTimeZone`) for it to show the name you want. – Michael Scheper Oct 10 '18 at 13:49

3 Answers3

13

It's up to you! You can use Locale.getDefault() or a Calendar instance.

Locale.getDefault() is not a TimeZone, but specify the language in device

  • Calendar calendar = Calendar.getInstance();
  • Locale.getDefault()

.

Below I present some examples:

Printing Log.d(LOG_TAG, String.valueOf(TimeZone.getDefault())); shows:

D/MyActivity: libcore.util.ZoneInfo[id="Brazil/East",mRawOffset=-10800000,mEarliestRawOffset=-10800000,mUseDst=true,mDstSavings=3600000,transitions=128]

.

Printing Log.d(LOG_TAG, TimeZone.getDefault().getID()); shows:

D/MyActivity: Brazil/East

.

Printing Log.d(LOG_TAG, String.valueOf(Locale.getDefault())); shows:

D/MyActivity: fr_FR

.

Printing Log.d(LOG_TAG, String.valueOf(calendar.getTimeZone())); shows:

D/MyActivity: libcore.util.ZoneInfo[id="Brazil/East",mRawOffset=-10800000,mEarliestRawOffset=-10800000,mUseDst=true,mDstSavings=3600000,transitions=128]

.

Printing Log.d(LOG_TAG, calendar.getTimeZone().getDisplayName()); shows:

D/MyActivity: heure normale de Brasilia

.

Printing timezone displayName without locale and Timezone SHORT:

Log.d(LOG_TAG, calendar.getTimeZone().getDisplayName(false, TimeZone.SHORT));

shows output:

D/MyActivity: GMT-03:00

.

Printing timezone displayName without locale and Timezone LONG

Log.d(LOG_TAG, calendar.getTimeZone().getDisplayName(false, TimeZone.LONG));

shows output:

D/MyActivity: heure normale de Brasilia

.

Check the Javadoc for the method you're using:

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Calendar.html#getInstance(java.util.Locale)

Gets a calendar using the default time zone and specified locale.

Check this comments to be more clear to you:

Francis Rodrigues
  • 1,470
  • 4
  • 25
  • 61
7

You can find out your current time zone by below code

private String getTimeZone() {
        Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
        TimeZone zone = calendar.getTimeZone();
        return zone.getID();
}
Mihir Patel
  • 404
  • 6
  • 14
3

Try this

TimeZone tz = TimeZone.getDefault();
System.out.println(tz.getID());

getDisplayName returns timezone and getId returns timezone location name.

Praveena
  • 6,340
  • 2
  • 40
  • 53
  • I tried. But this shows "Asia/Hong_Kong" instead of the exact location name. But thanks for your suggestion, it is a bit closer to my preferred way of presentation. – Antoine Murion Dec 15 '14 at 09:15
  • Asia/Hong_Kong _is_ the ID. What were you expecting? Are you sure it's the timezone name you're after, and not just the location? Hong Kong may be a big city, but it's just one city in a very populated timezone. – Michael Scheper Oct 10 '18 at 13:51