如何在 Java 中获得英国夏令时偏移 (BST) [英] How do I get British Summertime offset (BST) in Java

查看:57
本文介绍了如何在 Java 中获得英国夏令时偏移 (BST)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

代码

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

返回调试输出

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

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

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.

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

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.

Java 版本为 7.

The Java version is 7.

推荐答案

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

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.

0 格林威治标准时间的正确偏移量,永久.

0 is the correct offset for GMT, permanently.

您需要欧洲/伦敦",即在 GMT 和 BST 之间切换的时区.

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

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

I hadn't originally noticed that you're trying to 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:

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

这篇关于如何在 Java 中获得英国夏令时偏移 (BST)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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