从没有 TimeZone 名称的 TimeZone 获取 TimeZone 偏移值 [英] Get TimeZone offset value from TimeZone without TimeZone name

查看:31
本文介绍了从没有 TimeZone 名称的 TimeZone 获取 TimeZone 偏移值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以 [+/-]hh:mm 格式保存手机的时区

I need to save the phone's timezone in the format [+/-]hh:mm

我正在使用 TimeZone 类来处理这个问题,但我能得到的唯一格式如下:

I am using TimeZone class to deal with this, but the only format I can get is the following:

PST -05:00
GMT +02:00

我宁愿不对结果进行子串化,是否可以设置任何键或选项标志以仅获取值而不是该时区的名称(GMT/CET/PST...)?

I would rather not substring the result, is there any key or option flag I can set to only get the value and not the name of that timezone (GMT/CET/PST...)?

推荐答案

我需要以 [+/-]hh:mm 格式保存手机的时区

I need to save the phone's timezone in the format [+/-]hh:mm

不,你没有.单独的偏移量是不够的,您需要存储整个时区名称/id.例如,我住在奥斯陆,我目前的偏移量是 +02:00 但在冬天(由于 ) 它是 +01:00.标准时间和夏令时间之间的确切切换取决于您不想探索的因素.

No, you don't. Offset on its own is not enough, you need to store the whole time zone name/id. For example I live in Oslo where my current offset is +02:00 but in winter (due to dst) it is +01:00. The exact switch between standard and summer time depends on factors you don't want to explore.

所以不是存储 + 02:00(或者应该是 + 01:00?)我存储 "Europe/Oslo"在我的数据库中.现在我可以使用以下命令恢复完整配置:

So instead of storing + 02:00 (or should it be + 01:00?) I store "Europe/Oslo" in my database. Now I can restore full configuration using:

TimeZone tz = TimeZone.getTimeZone("Europe/Oslo")

想知道我今天的时区偏移量是多少?

Want to know what is my time zone offset today?

tz.getOffset(new Date().getTime()) / 1000 / 60   //yields +120 minutes

但是 12 月还是一样:

However the same in December:

Calendar christmas = new GregorianCalendar(2012, DECEMBER, 25);
tz.getOffset(christmas.getTimeInMillis()) / 1000 / 60   //yields +60 minutes

说得够多了:存储时区名称或 id 并且每次要显示日期时,检查当前偏移量(今天)是多少,而不是存储固定值.您可以使用 TimeZone.getAvailableIDs() 枚举所有支持的时区 ID.

Enough to say: store time zone name or id and every time you want to display a date, check what is the current offset (today) rather than storing fixed value. You can use TimeZone.getAvailableIDs() to enumerate all supported timezone IDs.

这篇关于从没有 TimeZone 名称的 TimeZone 获取 TimeZone 偏移值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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