Joda Time Interval between two dates including time zone

后端 未结 1 1764
隐瞒了意图╮
隐瞒了意图╮ 2021-02-20 02:59

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

DateTime time_server = new DateTime(server_time_milisecs).
    withZone(DateTimeZone.for         


        
相关标签:
1条回答
  • 2021-02-20 03:44

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

    For example, consider the following code

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

    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());
    

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

    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).

    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.

    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());
    

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

    0 讨论(0)
提交回复
热议问题