gpt4 book ai didi

java - 如何在 Java 中获取英国夏令时偏移量 (BST)

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:40:30 25 4
gpt4 key购买 nike

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

代码

    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”返回的偏移量为零,我找不到如何将其设置为返回正确偏移量的方法。

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

Java版本是7.

最佳答案

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 GMT 的永久正确偏移量。

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

编辑:我最初没有注意到您正在尝试到达欧洲/伦敦。看起来您的 JVM 使用的时区数据库基本上是一团糟。您可以尝试修复那个,或者简单地使用它自己的 tzdb 副本附带的 Joda Time。示例代码:

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

关于java - 如何在 Java 中获取英国夏令时偏移量 (BST),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12499318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com