我想在我的android程序中显示时区的“名称”。如果是"GMT+8:00",则显示"HongKong";通过搜索,我发现getDisplayName函数应该可以达到我的目的。
http://developer.android.com/reference/java/util/TimeZone.html#getDisplayName(boolean
然而,在我自己的程序中,这个函数只显示"GMT+8:00“,但是当我在Google Open Source Project中使用"getDisplayName”时,它会显示名称"HongKong“。
有人知道这背后的原因吗?
发布于 2017-03-03 00:26:43
由你决定!您可以使用Locale.getDefault()或Calendar实例。
Locale.getDefault()不是TimeZone,但在设备中指定语言
Calendar calendar = Calendar.getInstance();Locale.getDefault()。
下面我给出一些例子:
打印Log.d(LOG_TAG, String.valueOf(TimeZone.getDefault()));显示:
D/MyActivity: libcore.util.ZoneInfo[id="Brazil/East",mRawOffset=-10800000,mEarliestRawOffset=-10800000,mUseDst=true,mDstSavings=3600000,transitions=128]。
打印Log.d(LOG_TAG, TimeZone.getDefault().getID());显示:
D/MyActivity: Brazil/East
。
打印Log.d(LOG_TAG, String.valueOf(Locale.getDefault()));显示:
D/MyActivity: fr_FR
。
打印Log.d(LOG_TAG, String.valueOf(calendar.getTimeZone()));显示:
D/MyActivity: libcore.util.ZoneInfo[id="Brazil/East",mRawOffset=-10800000,mEarliestRawOffset=-10800000,mUseDst=true,mDstSavings=3600000,transitions=128]。
打印Log.d(LOG_TAG, calendar.getTimeZone().getDisplayName());显示:
D/MyActivity: heure normale de Brasilia
。
打印不带区域设置和时区短的时区displayName:
Log.d(LOG_TAG, calendar.getTimeZone().getDisplayName(false, TimeZone.SHORT));
显示输出:
D/MyActivity: GMT-03:00
。
打印不带区域设置和时区长度的时区displayName
Log.d(LOG_TAG, calendar.getTimeZone().getDisplayName(false, TimeZone.LONG));
显示输出:
D/MyActivity: heure normale de Brasilia
。
检查您正在使用的方法的Javadoc:
http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Calendar.html#getInstance(java.util.Locale)
使用默认时区和指定的区域设置获取日历。
请检查此注释,以使您更清楚:
发布于 2018-12-11 13:37:18
你可以通过下面的代码找出你当前的时区
private String getTimeZone() {
Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
TimeZone zone = calendar.getTimeZone();
return zone.getID();
}发布于 2014-12-15 16:57:17
尝尝这个
TimeZone tz = TimeZone.getDefault();
System.out.println(tz.getID());getDisplayName返回时区,getId返回时区位置名称。
https://stackoverflow.com/questions/27480053
复制相似问题