In this tutorial, we show you 2 examples to calculate date / time difference in Java :
- Manual time calculation.
- Joda time library.
1. Manual time calculation
Converts Date
in milliseconds (ms) and calculate the differences between two dates, with following rules:
1000 milliseconds = 1 second
60 seconds = 1 minute
60 minutes = 1 hour
24 hours = 1 day
Class DateDiferentExample.java
package br.com.ziben.date;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateDifferentExample {
public static void main(String[] args) {
String dateStart = "01/30/2013 09:29:58";
String dateStop = "01/31/2013 10:31:48";
// HH converts hour in 24 hours format (0-23), day calculation
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date d1 = null;
Date d2 = null;
try {
d1 = format.parse(dateStart);
d2 = format.parse(dateStop);
// in milliseconds
long diff = d2.getTime() - d1.getTime();
long diffSeconds = diff / 1000 % 60;
long diffMinutes = diff / (60 * 1000) % 60;
long diffHours = diff / (60 * 60 * 1000) % 24;
long diffDays = diff / (24 * 60 * 60 * 1000);
System.out.print(diffDays + " days, ");
System.out.print(diffHours + " hours, ");
System.out.print(diffMinutes + " minutes, ");
System.out.print(diffSeconds + " seconds.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Result…
1 days, 1 hours, 1 minutes, 50 seconds.
Why seconds and minutes need %60, and hours %24? If you change it to:
long diffSeconds = diff / 1000;
The result will be
1 days, 1 hours, 1 minutes, 90110 seconds.
The “90110
” is the total number of seconds difference between date1
and date2
, this is correct if you want to know the differences in seconds ONLY.
To display difference in “day, hour, minute and second” format, you should use a modulus (%60) to cut off the remainder of seconds (90060
). Got it? The idea is applied in minutes (%60) and hours (%24) as well.
90110 % 60 = 50 seconds (you want this)
90110 - 50 = 90060 seconds (you dont want this)
2. Joda Time Example
Here’s the equivalent example, but using Joda time to calculate differences between two dates.
P.S This example is using joda-time-2.1.jar
Class JodaDateDifferentExample.java
package br.com.ziben.date;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.joda.time.DateTime;
import org.joda.time.Days;
import org.joda.time.Hours;
import org.joda.time.Minutes;
import org.joda.time.Seconds;
public class JodaDateDifferentExample {
public static void main(String[] args) {
String dateStart = "01/30/2013 09:29:58";
String dateStop = "01/31/2013 10:31:48";
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date d1 = null;
Date d2 = null;
try {
d1 = format.parse(dateStart);
d2 = format.parse(dateStop);
DateTime dt1 = new DateTime(d1);
DateTime dt2 = new DateTime(d2);
System.out.print(Days.daysBetween(dt1, dt2).getDays() + " days, ");
System.out.print(Hours.hoursBetween(dt1, dt2).getHours() % 24 + " hours, ");
System.out.print(Minutes.minutesBetween(dt1, dt2).getMinutes() % 60 + " minutes, ");
System.out.print(Seconds.secondsBetween(dt1, dt2).getSeconds() % 60 + " seconds.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Result:
1 days, 1 hours, 1 minutes, 50 seconds.
Let me know if you have alternative ways 🙂