In this post we’ll see how to calculate difference between two dates in Java using SimpleDateFormat and using the new date and time API in Java 8.
Difference between two dates using SimpleDateFormat
Before Java 8 you could calculate difference between two dates manually using SimpleDateFormat.
public class DateDiff {
public static void main(String[] args) {
try {
dateDifference("28/02/2016 13:15:55", "01/03/2016 17:18:14", "dd/MM/yyyy HH:mm:ss");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void dateDifference(String date1, String date2, String pattern) throws ParseException{
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
Date d1 = sdf.parse(date1);
Date d2 = sdf.parse(date2);
long diffInMillis = d2.getTime() - d1.getTime();
long dateDiffInDays = TimeUnit.DAYS.convert(diffInMillis, TimeUnit.MILLISECONDS);
long dateDiffInHours = TimeUnit.HOURS.convert(diffInMillis - (dateDiffInDays * 24 * 60 * 60 * 1000), TimeUnit.MILLISECONDS);
long dateDiffInMinutes = TimeUnit.MINUTES.convert(diffInMillis - (dateDiffInDays * 24 * 60 * 60 * 1000) -(dateDiffInHours * 60 * 60 * 1000), TimeUnit.MILLISECONDS);
long dateDiffInSeconds = TimeUnit.SECONDS.convert(diffInMillis - (dateDiffInDays * 24 * 60 * 60 * 1000) - (dateDiffInHours * 60 * 60 * 1000) - (dateDiffInMinutes * 60 * 1000), TimeUnit.MILLISECONDS);
System.out.println(dateDiffInDays + " day(s) " + dateDiffInHours + " Hour(s) " + dateDiffInMinutes + " Minute(s) " + dateDiffInSeconds + " Second(s)");
}
}
Output
2 day(s) 4 Hour(s) 2 Minute(s) 19 Second(s)
Difference between two dates in Java 8
Java 8 onward you can use you can use new date and time API classes Period and Duration to find difference between two dates.
- Period class is used to model amount of time in terms of years, months and days.
- Duration class is used to model amount of time in terms of seconds and nanoseconds.
Using these two classes you can calculate difference between two dates in terms of years, months, days along with hours, minutes, seconds (for time component).
Difference between dates - Java code
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Period;
public class DateDiff {
public static void main(String[] args) {
LocalDateTime dateTime1 = LocalDateTime.of(2016, 8, 28, 13, 15, 55);
LocalDateTime dateTime2 = LocalDateTime.of(2016, 8, 29, 17, 18, 14);
getPeriod(dateTime1.toLocalDate(), dateTime2.toLocalDate());
getTime(dateTime1.toLocalTime(), dateTime2.toLocalTime());
}
/**
*
* @param date1
* @param date2
*/
private static void getPeriod(LocalDate date1, LocalDate date2){
Period p = Period.between(date1, date2);
System.out.println("Year " + p.getYears());
System.out.println("Months " + p.getMonths());
System.out.println("Days " + p.getDays());
}
/**
*
* @param time1
* @param time2
*/
private static void getTime(LocalTime time1, LocalTime time2){
Duration d = Duration.between(time1, time2);
long seconds = d.getSeconds();
//System.out.println("seconds " + seconds);
// Calculating hours
System.out.println("Hours " + d.toHours());
// Calculating Minutes
System.out.println("Minutes " + ((seconds % 3600)/60));
// Calculating Seconds
System.out.println("Seconds " + (seconds % 60));
}
}
Output
Year 0
Months 0
Days 1
Hours 4
Minutes 2
Seconds 19
Running it with another set of dates -
LocalDateTime dateTime1 = LocalDateTime.of(2016, 8, 28, 13, 15, 55);
LocalDateTime dateTime2 = LocalDateTime.of(2017, 10, 5, 15, 12, 59);
Output
Year 1
Months 1
Days 7
Hours 1
Minutes 57
Seconds 4
That's all for this topic Difference Between Two Dates in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!
Related Topics
You may also like -