简体   繁体   中英

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.

The expected output for the above inputs is 3600000 (60 * 60 * 1000) millisec but I'm getting 46800000 (13 * 60 * 60 * 1000).

When I went through different java forums people are suggesting to use 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 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( )

Creates a Seconds representing the number of whole seconds between the two specified partial datetimes. The two partials must contain the same fields, for example you can specify two LocalTime objects.

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. So to find miliseconds, you can use a constant storing LocalDateTime referring to Jan 1, 1970(Because of UNIX Time ).

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. Then calculate the difference between your 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.

Your local and utc represent the same instants of time, (only with different timezones attached). Hence, getMillis() (which gives the "physical" time interval elapsed from the "instant" corresponding to the unix epoch), must return the same value.

Joda is a perfect library but if you need the difference between 2 dates in milliseconds you just should calculate difference between getTime(). If you get wrong results you have some problems with timezones or so. Typically it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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