Calculate date/time difference in java

try

long diffSeconds = diff / 1000 % 60;  
long diffMinutes = diff / (60 * 1000) % 60; 
long diffHours = diff / (60 * 60 * 1000);

NOTE: this assumes that diff is non-negative.


If you are able to use external libraries I would recommend you to use Joda-Time, noting that:

Joda-Time is the de facto standard date and time library for Java prior to Java SE 8. Users are now asked to migrate to java.time (JSR-310).

Example for between calculation:

Seconds.between(startDate, endDate);
Days.between(startDate, endDate);

I would prefer to use suggested java.util.concurrent.TimeUnit class.

long diff = d2.getTime() - d1.getTime();//as given

long seconds = TimeUnit.MILLISECONDS.toSeconds(diff);
long minutes = TimeUnit.MILLISECONDS.toMinutes(diff); 

Tags:

Time

Java