如何从 Android 设备获取 GMT(如 GMT+7:00)的时区偏移量? [英] How to get the timezone offset in GMT(Like GMT+7:00) from android device?

查看:175
本文介绍了如何从 Android 设备获取 GMT(如 GMT+7:00)的时区偏移量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码获取 android 设备的 timezone

I am getting the timezone of a android device using this code

TimeZone tz = TimeZone.getDefault();
String current_Time_Zone = (TimeZone.getTimeZone(tz.getID()).getDisplayName(
                false, TimeZone.SHORT))

但它总是像IST"一样返回timezone,但我想像GMT那样在GMT中获取时区+7:00.

But it always return me the timezone like "IST" but i want to get the timezone in GMT like this GMT+7:00.

推荐答案

此代码返回我的 GMT 偏移量.

This code return me GMT offset.

Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"),
                Locale.getDefault());
Date currentLocalTime = calendar.getTime();
DateFormat date = new SimpleDateFormat("Z");
String localTime = date.format(currentLocalTime);

它像这样返回时区偏移量:+0530

It returns the time zone offset like this: +0530

如果我们在下面使用 SimpleDateFormat

If we use SimpleDateFormat below

DateFormat date = new SimpleDateFormat("z",Locale.getDefault());
String localTime = date.format(currentLocalTime);

它像这样返回时区偏移量:GMT+05:30

It returns the time zone offset like this: GMT+05:30

这篇关于如何从 Android 设备获取 GMT(如 GMT+7:00)的时区偏移量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆