获取军事时区缩写 [英] Get Military Time Zone Abbreviation
问题描述
我有一个 org.joda.time.DateTime
对象,我需要检索军事时区的缩写(例如UTC-的 T 07:00,对于UTC-08:00是 U ,对于UTC±00:00是 Z )。
I have a org.joda.time.DateTime
object and I need to retrieve the military time zone abbreviation (e.g. T for UTC-07:00, U for UTC-08:00, Z for UTC±00:00).
参见 http://en.wikipedia.org/wiki/List_of_military_time_zones
这是我当前的操作方式:
Here's how I'm currently doing it:
int offset = dt.getZone().toTimeZone().getRawOffset() / (60 * 60 * 1000);
String timeZoneCode = timeZoneCodeMap.get(offset);
其中 timeZoneCodeMap
是 HashMap< Integer,String>
,其初始化如下所示
where timeZoneCodeMap
is a HashMap<Integer, String>
that is initialized with entries like the following
timeZoneCodeMap.put(1, "A");
timeZoneCodeMap.put(2, "B");
timeZoneCodeMap.put(3, "C");
...
timeZoneCodeMap.put(-10, "W");
timeZoneCodeMap.put(-11, "X");
timeZoneCodeMap.put(-12, "Y");
timeZoneCodeMap.put(0, "Z");
是否存在已经包含时区映射的函数或库(无论是Joda还是其他形式)
Does there exist a function or library (in Joda or otherwise) that already contains a mapping of time zones to military abbreviations?
请随时告诉我是否还有更好的方法来计算偏移量。
Feel free to let me know if there is a better way to calculate the offset as well.
推荐答案
这是如此小的代码,您最好自己动手做...
This is such little code, that you might as well just do it yourself ...
static final String MAPPING = "YXWVUTSRQPONZABCDEFGHIKLM";
...
String timeZoneCode = MAPPING.substring(offset + 12, offset + 13);
这篇关于获取军事时区缩写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!