How to get time difference between 2 dates in milliseconds using JodaTime

I am going to create an application in which I need to get the exact time difference between two dates. Example:

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

I tried using getTime() , but I did not get the exact result.

The expected output for the above inputs is 3600000 (60 * 60 * 1000) milliseconds, but I get 46800000 (13 * 60 * 60 * 1000).

When I went through various java forums, people suggested using JodaTime.

However, I cannot get the exact result.

The time zone I work for is London (GMT).

+7
source share
5 answers
 public static long getDiff(Calender cal1, Calender cal2) { return Math.abs(cal1.getTimeInMillis() - cal2.getTimeInMillis()); } 
+5
source

Check out secondsBetween ()

Creates seconds representing the number of whole seconds between two specified partial times. These two parts must contain the same fields, for example, you can specify two LocalTime objects.

Options:

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

Return:

  the period in seconds 
+4
source

Initiate 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 the difference in milliseconds:

 System.out.println(p.getValue(0)); 
+4
source

JodaTime uses computer time internally. Thus, to find miliseconds, you can use LocalDateTime persistent storage with reference to January 1, 1970 (Due to UNIX Time ).

Unix time or POSIX time is a system for describing points in time, defined as the number of seconds elapsed from the midnight prolits Coordinated universal time (UTC) from January 1, 1970, not counting the 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:

 Europe/Amsterdam milis :1429695646528 UTC milis :1429692046534 

And @leonbloy write a good comment here .

Your local and utc represent the same time points (only from different timelines). Therefore, getMillis () (which gives the β€œphysical” time interval elapsed from the β€œmoment” corresponding to the unix epoch) should return the same value.

+3
source

Joda is the perfect library, but if you want the difference between two dates in milliseconds, you just need to calculate the difference between getTime (). If you make a mistake, you will have problems with time zones or so. This usually works.

+1
source

All Articles