java - 使用 Joda-Time 计算持续时间

标签 java timezone jodatime dst leap-year

我正在尝试使用 Joda-Time 来了解两个时间点之间的持续时间,其中每个点都在其自己的本地时区中给出。

例如:

DateTime ny = new DateTime(2011, 2, 2, 7, 0, 0, 0, DateTimeZone.forID("America/New_York"));
DateTime la = new DateTime(2011, 2, 3, 10, 15, 0, 0, DateTimeZone.forID("America/Los_Angeles"));
DateTime utc1 = ny.withZone(DateTimeZone.UTC);
DateTime utc2 = la.withZone(DateTimeZone.UTC);        
Period period = new Period(utc1, utc2);

现在,我想知道这是否考虑了夏令时和闰年...... 此外,使用“Period”是否是实现此目标的正确 Joda-Time 方法? 谢谢 ;)

最佳答案

您提供的代码可以使用并考虑时区,但您不需要转换为 UTC。这段代码更简单并且做同样的事情(使用 Duration 而不是 Period):

DateTime ny = new DateTime(2011, 2, 2, 7, 0, 0, 0, DateTimeZone.forID("America/New_York"));
DateTime la = new DateTime(2011, 2, 3, 10, 15, 0, 0, DateTimeZone.forID("America/Los_Angeles"));
Duration duration = new Interval(ny, la).toDuration();

关于java - 使用 Joda-Time 计算持续时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4983846/

相关文章:

java - 如何获取指定 ISO 国家代码的时区 ID?

serialization - 日期时间序列化器 : what is parameter shapeOverride for?

java - jackson 未能将字符串反序列化为 Joda-Time

java - 没有打印语句,循环看不到其他线程更改的值

java - 检索存储在数据库中的日期的时区

java 二维图形库

postgresql - 将时间戳转换为Postgres中特定时区的时间戳

java - 如何将 Joda 库的 DateTime 舍入到最接近的 X 分钟?

java - org.eclipse.jdt.core.dom.AST 的 API 使用示例

java - 为什么 File.getParent() 在 Windows 中返回不带斜杠的路径?