简体   繁体   English

如何使用 JodaTime 以毫秒为单位获取 2 个日期之间的时间差

[英]How to get the time difference between 2 dates in millisec using JodaTime

I'm going to design an application, in which I need to get the exact time difference between two dates.我将设计一个应用程序,我需要在其中获取两个日期之间的确切时间差。 Ex:前任:

Date1:31/05/2011 12:54:00
Date2:31/05/2011 13:54:00

I tried using getTime() but I didn't get exact result.我尝试使用getTime()但我没有得到确切的结果。

The expected output for the above inputs is 3600000 (60 * 60 * 1000) millisec but I'm getting 46800000 (13 * 60 * 60 * 1000).上述输入的预期 output 为3600000 (60 * 60 * 1000) 毫秒,但我得到46800000 (13 * 60 * 60 * 1000)。

When I went through different java forums people are suggesting to use JodaTime.当我浏览不同的 java 论坛时,人们建议使用 JodaTime。

Still I'm unable to get the exact result.我仍然无法得到确切的结果。

The timezone on I'm working is London(GMT).我工作的时区是伦敦(格林威治标准时间)。

Init two dateTime and use Period:初始化两个 dateTime 并使用 Period:

DateTime dt1 = new DateTime(2013,9,11,9,58,56);
DateTime dt2 = new DateTime(2013,9,11,9,58,59);
Period p = new Period(dt1, dt2, PeriodType.millis());

To get difference in milliseconds:以毫秒为单位获得差异:

System.out.println(p.getValue(0));
public static long getDiff(Calender cal1, Calender cal2)
{
    return Math.abs(cal1.getTimeInMillis() - cal2.getTimeInMillis());
}

Check out secondsBetween( )查看secondsBetween( )

Creates a Seconds representing the number of whole seconds between the two specified partial datetimes.创建一个 Seconds,表示两个指定的部分日期时间之间的整秒数。 The two partials must contain the same fields, for example you can specify two LocalTime objects.这两个部分必须包含相同的字段,例如您可以指定两个 LocalTime 对象。

Parameters:参数:

 start - the start partial date, must not be null
    end - the end partial date, must not be null 

Returns:回报:

 the period in seconds 

JodaTime is using machine time inside. JodaTime 在内部使用机器时间。 So to find miliseconds, you can use a constant storing LocalDateTime referring to Jan 1, 1970(Because of UNIX Time ).因此,要查找毫秒,您可以使用存储 LocalDateTime 的常量来表示 1970 年 1 月 1 日(因为UNIX 时间)。

Unix time, or POSIX time, is a system for describing points in time, defined as the number of seconds elapsed since midnight proleptic Coordinated Universal Time (UTC) of January 1, 1970, not counting leap seconds. Unix 时间或 POSIX 时间是一种用于描述时间点的系统,定义为自 1970 年 1 月 1 日午夜预测协调世界时 (UTC) 以来经过的秒数,不包括闰秒。 Then calculate the difference between your DateTime.然后计算您的 DateTime 之间的差异。

I tried like this;我试过这样;

public static void main(String[] args) {
        final LocalDateTime JAN_1_1970 = new LocalDateTime(1970, 1, 1, 0, 0);
        DateTime local = new DateTime().withZone(DateTimeZone.forID("Europe/Amsterdam"));
        DateTime utc = new DateTime(DateTimeZone.UTC);

        System.out.println("Europe/Amsterdam milis :" + new Duration(JAN_1_1970.toDateTime(DateTimeZone.forID("Europe/Amsterdam")), local).getMillis());
        System.out.println("UTC  milis             :" + new Duration(JAN_1_1970.toDateTime(DateTimeZone.UTC), utc).getMillis());

    }

And the result is;结果是;

Europe/Amsterdam milis :1429695646528
UTC  milis             :1429692046534

And @leonbloy write here a good comment.@leonbloy 在这里写一个很好的评论。

Your local and utc represent the same instants of time, (only with different timezones attached).您的 local 和 UTC 代表相同的时间瞬间(仅附有不同的时区)。 Hence, getMillis() (which gives the "physical" time interval elapsed from the "instant" corresponding to the unix epoch), must return the same value.因此,getMillis()(给出从对应于 unix 纪元的“瞬间”经过的“物理”时间间隔)必须返回相同的值。

Joda is a perfect library but if you need the difference between 2 dates in milliseconds you just should calculate difference between getTime(). Joda 是一个完美的库,但如果您需要以毫秒为单位的 2 个日期之间的差异,您只需计算 getTime() 之间的差异。 If you get wrong results you have some problems with timezones or so.如果你得到错误的结果,你会遇到一些时区问题。 Typically it works.通常它会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM