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


    方法: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();

  • 相关阅读:
    0426-mysql插入语句大全
    JS节点操作
    模态框
    滚动监听 after选择器
    JS数组
    js函数 DOM操作
    JS循环 for while 全局/局部变量 短路
    JavaScript 基础 if switch 弹窗 运算符
    无序列表属性 隐藏方式 JS简介
    Css问题 margin float 文档流 背景图底部充满
  • 原文地址:https://www.cnblogs.com/ryq2014/p/5546261.html
Copyright © 2020-2023  润新知