0

I have to implement a java program to make the difference between two hours. The problem is that my "hour pattern" is like this:

MM/dd/yyyy HH:mm:ss.SSS

I have to consider ms, too.

I tried using joda:

String sDateStart = "2015/10/14 22:37:28.648";
String sDateFiish = "2015/10/14 22:39:13.573";

SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss.SSS");

Date dt1 = format.parse(sDateFiish);
Date dt2 = format.parse(sDateStart);


DateTime start = new DateTime(dt1);
DateTime finish = new DateTime(dt2);

System.out.println(Days.daysBetween(start, finish));
System.out.println(Minutes.minutesBetween(start, finish));
System.out.println(Seconds.secondsBetween(start, finish));

Is there any method to consider the ms?

harry-potter
  • 1,981
  • 5
  • 29
  • 55

1 Answers1

0

If you are using Java API itself. In Date class we have getTime method which gives time in millisecond

getTime() Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.

System.out.println((long)(dt1.getTime() - dt2.getTime()));

If you wanna continue using Joda Time API use Duration. as mentioned by Sotirios Delimanolis

Acewin
  • 1,657
  • 4
  • 17
  • 36