两个日期之间的Joda时间间隔,包括时区 [英] Joda Time Interval between two dates including time zone

查看:154
本文介绍了两个日期之间的Joda时间间隔,包括时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用JodaTime库进行时间操作. 我有两个约会: 日期一:

I use JodaTime library for time operations. I have two dates: Date One:

DateTime time_server = new DateTime(server_time_milisecs).
    withZone(DateTimeZone.forID("Europe/Zurich"));  //+0100

显示:2013-01-27 13:44:42

第二天:

DateTime time_local = new DateTime(DateTime.now()).
    withZone(DateTimeZone.getDefault());    //Have "Europe/Moscow" timezone  +0400

显示:2013-01-27 16:41:47

我需要找到包括时区在内的实际间隔

Interval interval = new Interval(time_local, time_server);
Long.toString(interval.toDurationMillis()));

结果:1​​74040毫秒-不正确

Result: 174040 miliseconds - NOT CORRECT

int secs = Seconds.secondsBetween(time_local,time_server).getSeconds();

结果:1​​74秒不正确

Period period = new Period(time_local, time_server);
Integer.toString(period.getSeconds()) //**Wrong too**

结果必须为:10974秒

推荐答案

您应该花一些时间(不要双关语)来掌握Jodatime的概念(

You should spend some time (no pun intended) to grasp the Jodatime concepts (eg) if you need to do time calculations involving different timezones.

例如,考虑以下代码

DateTime nowHere = DateTime.now();
DateTime nowZur = nowHere.withZone(DateTimeZone.forID("Europe/Zurich"));
System.out.println("now Here:   " + nowHere );
System.out.println("now Zurich: " + nowZur  );

此输出(对我来说,距离苏黎世有4个小时的时间):

This outputs (for me, 4 hours offset from Zurich) :

now Here:   2013-01-27T10:19:24.460-03:00
now Zurich: 2013-01-27T14:19:24.460+01:00

暂停并尝试猜测以下行的输出:

Pause and try to guess the output of the following lines:

Interval interv = new Interval(nowHere, nowZur);
System.out.println("Interval: " + interv.toDurationMillis());

以上打印0(零).应当.

The above prints 0 (zero). As it should.

因为nowHerenowZur表示同一时刻(在物理线路时间中),如在两个不同的国家/地区中所表示.它们仅在表示方式上有所不同,但在物理上它们是相同的时间点(例如2.54 cm1 in具有相同的长度,以两种不同的形式表示).

Because nowHere and nowZur represent the same instant of time (in the physical line time), as represented in two different countries. They differ only in how they are represented, but physically they are the same time point (like 2.54 cm and 1 in are the same length, represented in two different forms).

同样,2013-01-27T10:19:24.460-03:002013-01-27T14:19:24.460+01:00是同一时刻,即我运行该代码的时刻,仅根据不同国家/地区的约定表示;火星人可以用自己的火星日历表示同一时刻,但仍然是同一时刻.

Likewise, 2013-01-27T10:19:24.460-03:00 and 2013-01-27T14:19:24.460+01:00 are the same instant, the moment in which I run that code, only represented according the conventions of different countries; a martian could represent that same instant with his own martian calendar, and it would still be the same instant.

由于那些DateTimes表示相同的时间点,因此它们之间的物理间隔(持续时间)必须为零.现在,如果您想获得两者之间的民事时差",那是完全不同的事情.那么LocalDateTimePeriod(与DateTimeDuration完全不同)将是正确的概念.例如:

As those DateTimes represent the same time point, the physical interval (duration) between them must be zero. Now, if you want to get the "civil time difference" between the two, that's a wholly different thing. The LocalDateTime and the Period (totally different from DateTime and Duration) would then be the right concepts. For example:

Period per=new Period(nowHere.toLocalDateTime(),nowZur.toLocalDateTime());
System.out.println(per.toStandardSeconds().getSeconds());

这将为我打印14400(4个标准"-不是体力工时)

This prints 14400 for me (4 "standard" -not physical- hours)

这篇关于两个日期之间的Joda时间间隔,包括时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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