我如何在 Java 中获得英国夏令时偏移 (BST)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12499318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:09:28  来源:igfitidea点击:

How do I get British Summertime offset (BST) in Java

javatimezone

提问by Oliver Kohll

In the UK, I'd like to get the current offset from UTC/GMT. Currently the offset is 1 hour, but there seems to be no way of finding this.

在英国,我想从 UTC/GMT 获取当前偏移量。目前偏移量为 1 小时,但似乎无法找到它。

Code

代码

    TimeZone timeZone = TimeZone.getDefault();
    logger.debug("Timezone ID is '" + timeZone.getID() + "'");
    if (timeZone.getID().equals("GMT")) {
        timeZone = TimeZone.getTimeZone("Europe/London");
        logger.debug("New timezone is '" + timeZone.getID() + "'");
    }
    Long eventDateMillis = Long.parseLong(eventDateValue.getKeyValue());
    int timezoneOffset = timeZone.getOffset(eventDateMillis);
    logger.debug("Offset for " + eventDateValue + "(" + eventDateMillis + ") using timezone " + timeZone.getDisplayName() + " is " + timezoneOffset);

returns debug output

返回调试输出

Wed 2012/09/19 16:38:19.503|Debug|DataManagement|Timezone ID is 'GMT'
Wed 2012/09/19 16:38:19.503|Debug|DataManagement|New timezone is 'GMT'
Wed 2012/09/19 16:38:19.557|Debug|DataManagement|Offset for 18 Sep 2012 09:00(1347958800000) using timezone Greenwich Mean Time is 0

In other words, timezone 'GMT' is returning an offset of zero and I can't find how to set it to something that does return the correct offset.

换句话说,时区“GMT”返回的偏移量为零,我找不到如何将其设置为确实返回正确偏移量的内容。

If someone can provide a working code sample for JodaTime, that would do but I'd really like to avoid having to install a separate library just for what should be a one-liner.

如果有人可以为 JodaTime 提供一个工作代码示例,那就可以了,但我真的很想避免为应该是单行的而安装单独的库。

The Java version is 7.

Java 版本是 7。

回答by Jon Skeet

In other words, timezone 'GMT' is returning an offset of zero and I can't find how to set it to something that does return the correct offset.

换句话说,时区“GMT”返回的偏移量为零,我找不到如何将其设置为确实返回正确偏移量的内容。

0 isthe correct offset for GMT, permanently.

0GMT 的正确偏移量,永久。

You want "Europe/London", which is the time zone which switches between GMT and BST.

您需要“欧洲/伦敦”,这是在 GMT 和 BST 之间切换的时区。

EDIT: I hadn't originally noticed that you're tryingto get Europe/London. It looks like the time zone database your JVM is using is basically messed up. You could either try fixing that, or simply use Joda Time which comes with its own copy of tzdb. Sample code:

编辑:我最初没有注意到您正在尝试获取欧洲/伦敦。看起来您的 JVM 使用的时区数据库基本上搞砸了。您可以尝试修复该问题,也可以简单地使用 Joda Time 自带的 tzdb 副本。示例代码:

DateTimeZone zone = DateTimeZone.forID("Europe/London");
long offset = zone.getOffset(new Instant());
System.out.println(offset); // 3600000

回答by user1016765

I just encountered this myself.

我自己刚遇到这个。

I tried TimeZone.getTimeZone("London"); and it worked.

我试过 TimeZone.getTimeZone("London"); 它奏效了。

No I have no idea why - 'London' isn't one of the IDs TimeZone returns...

不,我不知道为什么 - “伦敦”不是 TimeZone 返回的 ID 之一......

回答by Jesse Barnum

Use timezone.getOffset( 0 ), instead of timezone.getOffset( eventDateMillis ).

使用 timezone.getOffset( 0 ),而不是 timezone.getOffset( eventDateMillis )。

This will give you the offset as of January 1st, 1970 GMT time, which in the case of Europe/London is 3600000, NOT zero due to historical issues with Wales and Northern Ireland.

这将为您提供截至格林威治标准时间 1970 年 1 月 1 日的偏移量,在欧洲/伦敦的情况下为 3600000,由于威尔士和北爱尔兰的历史问题而不是零。

回答by 4lberto

This made me the trick:

这让我有了诀窍:

String zone = "Europe/London";
int plusHoursGMT = TimeZone.getTimeZone(zone).getRawOffset() / 3600000;
assertThat(plusHoursGMT, is(0));