您的位置:首页 > 移动开发 > Android开发

[Android]如何获取当前用户设置的时区

2016-05-31 16:17 531 查看
方法:TimeZone.getDefault().getDisplayName(true, TimeZone.SHORT);
获取的值如GMT+08:00,GMT-04:00,EDT

另附:
国家码查询网址:http://www.chemie.fu-berlin.de/diverse/doc/ISO_3166.html
时区查询网址:https://en.wikipedia.org/wiki/List_of_tz_database_time_zones

说明:
参加源码TimeZone.java


public final String getDisplayName() {
return getDisplayName(false, LONG, Locale.getDefault());
}
public final String getDisplayName(Locale locale) {
return getDisplayName(false, LONG, locale);
}
public final String getDisplayName(boolean daylightTime, int style) {
return getDisplayName(daylightTime, style, Locale.getDefault());
}

/**
* Returns the {@link #SHORT short} or {@link #LONG long} name of this time
* zone with either standard or daylight time, as written in {@code locale}.
* If the name is not available, the result is in the format
* {@code GMT[+-]hh:mm}.
*
* @param daylightTime true for daylight time, false for standard time.
* @param style either {@link TimeZone#LONG} or {@link TimeZone#SHORT}.
* @param locale the display locale.
*/
public String getDisplayName(boolean daylightTime, int style, Locale locale) {
if (style != SHORT && style != LONG) {
throw new IllegalArgumentException("Bad style: " + style);
}

String[][] zoneStrings = TimeZoneNames.getZoneStrings(locale);
String result = TimeZoneNames.getDisplayName(zoneStrings, getID(), daylightTime, style);
if (result != null) {
return result;
}

// If we get here, it's because icu4c has nothing for us. Most commonly, this is in the
// case of short names. For Pacific/Fiji, for example, icu4c has nothing better to offer
// than "GMT+12:00". Why do we re-do this work ourselves? Because we have up-to-date
// time zone transition data, which icu4c _doesn't_ use --- it uses its own baked-in copy,
// which only gets updated when we update icu4c. http://b/7955614 and http://b/8026776. 
// TODO: should we generate these once, in TimeZoneNames.getDisplayName? Revisit when we
// upgrade to icu4c 50 and rewrite the underlying native code. See also the
// "element[j] != null" check in SimpleDateFormat.parseTimeZone, and the extra work in
// DateFormatSymbols.getZoneStrings.
int offsetMillis = getRawOffset();
if (daylightTime) {
offsetMillis += getDSTSavings();
}
return createGmtOffsetString(true /* includeGmt */, true /* includeMinuteSeparator */,
offsetMillis);
}


public int getDSTSavings() {
return useDaylightTime() ? 3600000 : 0;
}
/**
* Returns the offset in milliseconds from UTC of this time zone's standard
* time.
*/
public abstract int getRawOffset();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: