Get Military Time Zone Abbreviation

旧巷老猫 提交于 2019-12-08 14:39:57

问题


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).

See 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);

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");

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.


回答1:


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);


来源:https://stackoverflow.com/questions/19965056/get-military-time-zone-abbreviation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!