我正在尝试使用 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/