从TimeZone获取TimeZone偏移值而不使用TimeZone名称 [英] Get TimeZone offset value from TimeZone without TimeZone name
问题描述
我需要以格式[+/-] 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但是在冬天(由于 dst )它是+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
in我的数据库。现在我可以使用以下方法恢复完整配置:
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屋!