How to calculate difference between two dates in Java
On this page, we are going to calculate the difference between two dates. Sometimes we have a requirement in our project to process the calculation or update any page on the differences of date.
Here we are going to discuss a simple scenario where we need to calculate the difference between two dates. Suppose retrieve the recode form table and display it on JSP and comes in tabular form and have a date column. Mark entire those row where difference btw date column in table and current is greater than seven.
Now we are going to use TimeUnit class of java.util.concurrent package. A TimeUnit represents time durations at a given unit of granularity and provides utility methods to convert across units and to perform timing and delay operations in these units. TimeUnit class available since JDK 1.5.
Calculation with TimeUnit class
package org.websparrow;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;
public class DateDiffExp1 {
public static void main(String[] args) {
// set the new date format
DateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy");
Date currentDate = new Date();
Date date1 = null;
Date date2 = null;
try {
// calculating the difference b/w startDate and endDate
String startDate = "01-01-2016";
String endDate = simpleDateFormat.format(currentDate);
date1 = simpleDateFormat.parse(startDate);
date2 = simpleDateFormat.parse(endDate);
long getDiff = date2.getTime() - date1.getTime();
// using TimeUnit class from java.util.concurrent package
long getDaysDiff = TimeUnit.MILLISECONDS.toDays(getDiff);
System.out.println("Differance between date " + startDate + " and " + endDate + " is " + getDaysDiff + " days.");
} catch (Exception e) {
e.printStackTrace();
}
}
}Output:
Differance between date 01-01-2016 and 26-12-2016 is 360 days.Manual calculation
In this example, we are going to calculate the date difference using manual calculation. Before calculating manually these units must be known.
A nanosecond is defined as one thousandth of a microsecond, a microsecond as one thousandth of a millisecond, a millisecond as one thousandth of a second, a minute as sixty seconds, an hour as sixty minutes, and a day as twenty four hours.
24 hours = 1 day
60 minutes = 1 hour
60 seconds = 1 minute
1000 milliseconds = 1 second
package org.websparrow;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateDiffExp {
public static void main(String[] args) {
DateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy");
Date currentDate = new Date();
Date date1 = null;
Date date2 = null;
try {
String startDate = "01-01-2016";
String endDate = simpleDateFormat.format(currentDate);
date1 = simpleDateFormat.parse(startDate);
date2 = simpleDateFormat.parse(endDate);
long getDiff = date2.getTime() - date1.getTime();
long getDaysDiff = getDiff / (24 * 60 * 60 * 1000);
System.out.println("Differance between date " + startDate + " and " + endDate + " is " + getDaysDiff + " days.");
} catch (Exception e) {
e.printStackTrace();
}
}
}Output:
Differance between date 01-01-2016 and 26-12-2016 is 360 days.