Reputation: 813
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?
Upvotes: 12
Views: 13190
Reputation: 1510
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:
Upvotes: 13
Reputation: 412
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();
}
Upvotes: 7
Reputation: 6941
Try this
TimeZone tz = TimeZone.getDefault();
System.out.println(tz.getID());
getDisplayName
returns timezone and getId
returns timezone location name.
Upvotes: 4