我的日期格式为20Jan2013,08Aug2012等,具有自己的特定时区.例如,20Jan2013可能具有澳大利亚/墨尔本的时区ID,而08Aug2012可能具有欧洲/伦敦的ID.我想要做的是,根据这些时区和日期,计算给定日期该时区的UTC偏移量.到目前为止我已经想出了这个:
DateTimeFormatter dtf = DateTimeFormat.forPattern("ZZ");
DateTimeFormatter dtf1 = DateTimeFormat.forPattern("ddMMMYYYY");
DateTimeZone zone = DateTimeZone.forID("Australia/Melbourne");
DateTime thisDate = dtf1.parseDateTime("30Jul2013");
System.out.println("\nZone: " + thisDate.withZone(zone));
Run Code Online (Sandbox Code Playgroud)
这给了我输出:
Zone: 2013-07-30T00:00:00.000+10:00
Run Code Online (Sandbox Code Playgroud)
这是正确的,但我想从中提取UTC偏移量,在这种情况下为+10:00.我已经找到了办法,但找不到任何东西.有什么方法可以做到这一点吗?我看到的唯一选择是将输出转换为String并使用substring方法获取UTC偏移量.
上面的代码确实考虑了夏令时(夏令时).所以例如,如果我有:DateTime thisDate = dtf1.parseDateTime("30Jan2013");
输出将是:2013-01-30T00:00:00.000 + 11:00
(+11:00而不是+10:00)
所以基本上我需要做的是找到一种从2013-07-30T00:00:00.000 + 11:00提取+11:00的方法.请帮忙!