使用 Joda 时间获取给定时区的当前墙上时间

发布于 2025-01-03 14:24:20 字数 1483 浏览 1 评论 0原文

要求只是获取给定时区的当前挂机时间(包括正确的 DST 调整)。

SO 中似乎有一些问题围绕着这个问题徘徊,但我似乎无法找到一个直接的答案(在 SO、Joda doco 或谷歌搜索中)以低摩擦的方式获取挂机时间。似乎有了给定的输入(当前 UTC 时间和所需的 TZ),我应该能够链接 Joda Time 库中的几个方法来实现我想要的,但在上述示例中似乎希望评估+处理偏移量/应用程序代码中的转换 - 如果可能的话,我想避免这种情况,并根据其可用的静态 TZ 规则集尽最大努力使用 Jodas。

出于此问题的目的,我们假设我不会使用任何其他第三方服务(基于网络或其他二进制文件),而仅使用 JDK 和 JodaTime 库中提供的服务。

任何指示表示赞赏。

更新: 这实际上是我的失态。我根据请求经度计算了 UTC 偏移量,虽然这显然有效,但您仍然需要区域信息才能获得正确的 DST 调整..

double aucklandLatitude = 174.730423;
int utcOffset = (int) Math.round((aucklandLatitude * DateTimeConstants.HOURS_PER_DAY) / 360);
System.out.println("Offset: " + utcOffset);

DateTimeZone calculatedDateTimeZone = DateTimeZone.forOffsetHours(utcOffset);
System.out.println("Calculated DTZ: " + calculatedDateTimeZone);
System.out.println("Calculated Date: " + new DateTime(calculatedDateTimeZone));
System.out.println();
DateTimeZone aucklandDateTimeZone = DateTimeZone.forID("Pacific/Auckland");
System.out.println("Auckland DTZ: " +  aucklandDateTimeZone);
System.out.println("Auckland Date: " + new DateTime(aucklandDateTimeZone));

打印

Offset: 12
Calculated DTZ: +12:00
Calculated Date: 2012-02-08T11:20:04.741+12:00

Auckland DTZ: Pacific/Auckland
Auckland Date: 2012-02-08T12:20:04.803+13:00

所以在阳光明媚的地方 新西兰奥克兰我们在 DST 期间为 +12,但为 +13。

我的不好。尽管如此,还是感谢您的回答,让我看到了自己的错误。

The requirement is to simply to get the current wall time (including the correct DST adjustment) for a given Time Zone.

There seems to be a few questions in SO hovering around this, but I can't seem to find a straight answer (in SO, Joda doco or googling) with a low friction way of getting the wall time. It seems like with the given inputs (current UTC time and desired TZ) I should be able to chain a couple methods from the Joda Time library to achieve what I want but there seems to be a desire in said examples to evaluate + handle offsets / transitions in application code - I want to avoid this if possible and just use Jodas best effort based on its set of static TZ rules available.

For the purposes of this question, lets assume that I wont be using any other third party services (network based or other binaries), just what is available from the JDK and JodaTime library.

Any pointers appreciated.

Update:
This was actually a gaffe on my behalf. I had a calculated UTC offset based on the requests longitude, while this works obviously you still need regional information to get the correct DST adjustment..

double aucklandLatitude = 174.730423;
int utcOffset = (int) Math.round((aucklandLatitude * DateTimeConstants.HOURS_PER_DAY) / 360);
System.out.println("Offset: " + utcOffset);

DateTimeZone calculatedDateTimeZone = DateTimeZone.forOffsetHours(utcOffset);
System.out.println("Calculated DTZ: " + calculatedDateTimeZone);
System.out.println("Calculated Date: " + new DateTime(calculatedDateTimeZone));
System.out.println();
DateTimeZone aucklandDateTimeZone = DateTimeZone.forID("Pacific/Auckland");
System.out.println("Auckland DTZ: " +  aucklandDateTimeZone);
System.out.println("Auckland Date: " + new DateTime(aucklandDateTimeZone));

prints

Offset: 12
Calculated DTZ: +12:00
Calculated Date: 2012-02-08T11:20:04.741+12:00

Auckland DTZ: Pacific/Auckland
Auckland Date: 2012-02-08T12:20:04.803+13:00

So here in sunny Auckland, NZ we are +12 but +13 in the DST period.

My bad. Thanks for the answers nonetheless, made me see my mistake.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

多孤肩上扛 2025-01-10 14:24:20

您是否看过 DateTime 构造函数:

DateTime(DateTimeZone zone) 

它构造一个表示指定时区当前时间的 DateTime。

Have you looked at the DateTime constructor:

DateTime(DateTimeZone zone) 

This constructs a DateTime representing the current time in the specified timezone.

北笙凉宸 2025-01-10 14:24:20

怎么样:

DateTime utc = new DateTime(DateTimeZone.UTC);
DateTimeZone tz = DateTimeZone.forID("America/Los_Angeles");
DateTime losAngelesDateTime = utc.toDateTime(tz);

How about:

DateTime utc = new DateTime(DateTimeZone.UTC);
DateTimeZone tz = DateTimeZone.forID("America/Los_Angeles");
DateTime losAngelesDateTime = utc.toDateTime(tz);
还如梦归 2025-01-10 14:24:20

最干净的方法

我发现以下是最干净的方法。

DateTime currentTime = DateTime.now( DateTimeZone.UTC );

这将获取 UTC 中的当前时间,但该值可以转换为另一个 DateTimeZone,或者您可以将 DateTimeZone.UTC 替换为不同的 >日期时区

使用系统时区

如果你想将其设置为系统时区,你可以使用:

DateTime currentTime = DateTime.now( DateTimeZone.getDefault() );

Cleanest Method

I find the following to be the cleanest method.

DateTime currentTime = DateTime.now( DateTimeZone.UTC );

This gets you the current time in UTC but this value can be converted to another DateTimeZone or you can replace DateTimeZone.UTC with a different DateTimeZone.

Using the System TimeZone

If you want to set it to the system timezone, you can use this:

DateTime currentTime = DateTime.now( DateTimeZone.getDefault() );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文