使用Joda-Time在时区中获取本地毫秒数的最简单方法 [英] Simplest way to get local milliseconds in a time zone with Joda-Time

查看:240
本文介绍了使用Joda-Time在时区中获取本地毫秒数的最简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,要在1970年开始的本地时区获得毫秒数,

Currently, to get milliseconds from start of 1970 in a local time zone, I do

long localMillis = dateTime.withZone(timeZone).toLocalDateTime()
    .toDateTime(DateTimeZone.UTC).getMillis();

这可行,但是有更简单的方法吗?

This works, but is there a simpler way to do this?

推荐答案

通过存储引用1970年1月1日的常量LocalDateTime,然后在该时间点之间计算Duration,可以使这一点更加清晰(在给定的时区)和您关心的时刻,例如:

You can make this a little clearer by storing a constant LocalDateTime referring to Jan 1, 1970, and then calculating a Duration between that point in time (for a given time zone) and the instant that you care about, like:

private static final LocalDateTime JAN_1_1970 = new LocalDateTime(1970, 1, 1, 0, 0);

...

new Duration(JAN_1_1970.toDateTime(someTimeZone), endPointInstantOrDateTime).getMillis();

这篇关于使用Joda-Time在时区中获取本地毫秒数的最简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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