[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();
相关文章推荐
- Fragment之初级
- Android SD卡路径问题
- 单独编译Android源代码中的模块
- Android提高第三篇之SurfaceView与多线程的混搭
- Android Studio SVN配置忽略文件
- Android百度地图开发(一)之初体验
- LeakCanary: 让内存泄露无所遁形
- 在Android Studio中使用Android Tests(Junit)
- 深入Android MediaPlayer的使用方法详解
- android动画之逐帧动画
- 使用TabLayout、ViewPager和Fragment实现顶部菜单可滑动切换
- Android 对话框封装(确认/取消 )
- 关于Android xml 绘图
- Android 反编译工具 jadx
- Android 自定义View (四) 视频音量调控
- 基于Service的播放器,Activity,Service,BroadcastReceiver综合使用
- android工程下assets与raw文件夹的区别
- 菜鸟的安卓开发之路 --- android studio 的安装配置
- Android选择弹窗(自定义可扩展)
- android源码编译sdk