Android获取自定义格式时区

1 前言        

        我们知道,对于时区来说,有着不同标准,比如:GMT和UTC,而且还存在夏令时等一些让人疑惑的概念。那么,对于Android开发来说,想获取到合适的时区,应该怎么做呢?

        通常的,Android获取到的时区是使用Java的获取方式,默认获取GMT的标准。那么,根据返回时区的格式类型,一般有如下两种方式获取:

1.1 格式1:GMT+08:00

TimeZone.getDefault().getDisplayName(false, TimeZone.SHORT)

        上面是常见的获取GMT时区的方式,但是会存在返回“PST”、“WIB”等别名的时区,这会导致返回格式不统一,解析起来会存在问题。

1.2 格式2:+0800

SimpleDateFormat("Z").format(Calendar.getInstance(TimeZone.getTimeZone("GMT"),Locale.getDefault()).time)

        其实这种格式已经是比较通用,业务上可以直接使用这种方式统一时区格式。

2 自定义格式:GMT+08:00

        针对我的业务,已经定好了GMT+08:00这种格式,但是使用格式1存在着返回格式不统一的问题。

        全网并未找到合适的自定义格式方式,于是去看源码类TimeZone,发现了一个方法createGmtOffsetString和我的需求很像,诸君请看:

    /**
     * Returns a string representation of an offset from UTC.
     *
     * <p>The format is "[GMT](+|-)HH[:]MM". The output is not localized.
     *
     * @param includeGmt true to include "GMT", false to exclude
     * @param includeMinuteSeparator true to include the separator between hours     and minutes, false
     *     to exclude.
     * @param offsetMillis the offset from UTC
     *
     * @hide used internally by SimpleDateFormat
     */
    public static String createGmtOffsetString(boolean includeGmt,
            boolean includeMinuteSeparator, int offsetMillis)

        [GMT](+|-)HH[:]MM格式:includeGmt为true则显示“GMT”文本,includeMinuteSeparator为true则显示“:”。

        此时可根据业务需求去自定义。但这个方法是hide,我们可依葫芦画瓢重写一波此方法即可。

如下所示:

object LanguageUtils {

    /**
     * 获取当前时区
     * +0800        SimpleDateFormat("Z").format(Calendar.getInstance(TimeZone.getTimeZone("GMT"),Locale.getDefault()).time)
     * GMT+08:00    TimeZone.getDefault().getDisplayName(false, TimeZone.SHORT)——会存在PST、WIB直接别名的时区,后端不好过滤,赞、暂不采用
     * GMT+08:00    参考TimeZone方法createGmtOffsetString自定义方式实现
     * @return GMT+08:00
     */
    @JvmStatic
    fun getCurrentTimeZone(): String {
        return createGmtOffsetString(true, true, TimeZone.getDefault().rawOffset)
    }

    /**
     * Returns a string representation of an offset from UTC.
     *
     * <p>The format is "[GMT](+|-)HH[:]MM". The output is not localized.
     *
     * @param includeGmt true to include "GMT", false to exclude
     * @param includeMinuteSeparator true to include the separator between hours and minutes, false
     *     to exclude.
     * @param offsetMillis the offset from UTC
     *
     * @hide used internally by SimpleDateFormat
     */
    fun createGmtOffsetString(
        includeGmt: Boolean,
        includeMinuteSeparator: Boolean,
        offsetMillis: Int
    ): String {
        var offsetMinutes = offsetMillis / 60000
        var sign = '+'
        if (offsetMinutes < 0) {
            sign = '-'
            offsetMinutes = -offsetMinutes
        }
        val builder: StringBuilder = StringBuilder(9)
        if (includeGmt) {
            builder.append("GMT")
        }
        builder.append(sign)
        appendNumber(builder, 2, offsetMinutes / 60)
        if (includeMinuteSeparator) {
            builder.append(':')
        }
        appendNumber(builder, 2, offsetMinutes % 60)
        return builder.toString()
    }

    private fun appendNumber(builder: StringBuilder, count: Int, value: Int) {
        val string = value.toString()
        for (i in 0 until count - string.length) {
            builder.append('0')
        }
        builder.append(string);
    }

}

3 小结

        重写源码类TimeZone的createGmtOffsetString方法即可。

赞一波,源码YYDS。。。

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值