使用Joda-Time获取给定日期和时区的UTC偏移量 [英] Using Joda-Time to get UTC offset for a given date and timezone

查看:468
本文介绍了使用Joda-Time获取给定日期和时区的UTC偏移量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的日期格式为20Jan2013,08Aug2012等,具有自己的特定时区。例如,20Jan2013可能具有澳大利亚/墨尔本的时区ID,而08Aug2012可能具有欧洲/伦敦的ID。我想要做的是,根据这些时区和日期,计算给定日期该时区的UTC偏移量。到目前为止我已经想出了这个:

I have dates in the format 20Jan2013, 08Aug2012 etc, with their own specific timezones. So for example, 20Jan2013 might have a timezone ID of Australia/Melbourne, and 08Aug2012 might have an ID of Europe/London. What I want to do is, based on these timezones and the dates, calculate the UTC offset for that timezone on the given date. I've come up with this so far:

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

这给了我输出:

Zone: 2013-07-30T00:00:00.000+10:00

这是正确的,但我想从中提取UTC偏移量,在这种情况下为+10:00。我已经找到了办法,但找不到任何东西。有什么方法可以做到这一点吗?我看到的唯一选项是将输出转换为String并使用substring方法获取UTC偏移量。

This is correct, but I would like to extract just the UTC offset from this, which in this case is +10:00. I've looked for ways to do this but can't find anything. Is there any way I can do this? The only option I see is to convert the output to a String and use the substring method to get the UTC offset.

以上代码确实需要DST(夏令时)考虑到了。所以例如,如果我有:
DateTime thisDate = dtf1.parseDateTime(30Jan2013​​);

The above code does take DST (Daylight Saving Time) into account. So for example if I had: DateTime thisDate = dtf1.parseDateTime("30Jan2013");

输出为:2013-01-30T00:00:00.000 + 11:00

The output would be: 2013-01-30T00:00:00.000+11:00

(+最后11:00而不是+10:00

(+11:00 at the end instead of +10:00)

所以基本上我需要做的就是找到一种方法从2013-07-提取+11:00- 30T00:00:00.000 + 11:00。请帮助!

So basically all I need to do is find a way to extract +11:00 from 2013-07-30T00:00:00.000+11:00. Please help!

推荐答案



获取时区名称和偏移量的简单方法



Simple Method for Obtaining Timezone Name and Offset in Hours

public static String getCurrentTimeZoneOffset() {
    DateTimeZone tz = DateTimeZone.getDefault();
    Long instant = DateTime.now().getMillis();

    String name = tz.getName(instant);

    long offsetInMilliseconds = tz.getOffset(instant);
    long hours = TimeUnit.MILLISECONDS.toHours( offsetInMilliseconds );
    String offset = Long.toString( hours );

    return name + " (" + offset + " Hours)";
    // Example: "Mountain Standard Time (-7 Hours)"
}



< h3>情节警告:


  • 这从 JodaTime获取默认 DateTimeZone 即可。您可以修改它以接受传递给方法的特定DateTimeZone。

  • 这将以山区标准时间(-7小时)之类的格式返回但你可以很容易地将它格式化。

  • Couple caveats:

    • This gets the default DateTimeZone from JodaTime. You can modify it to accept a specific DateTimeZone that is passed into the method.
    • This returns it in a format like "Mountain Standard Time (-7 Hours)" but you can format it as you see fit quite easily.
    • 希望有所帮助。

      JP

      这篇关于使用Joda-Time获取给定日期和时区的UTC偏移量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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