In my previous article, I wrote about adding days to an instance of a date in Java. In this article, you'll learn how to calculate the difference between two dates in Java using Java 8 new date and time API as we ll as the legacy API.

Java 8 Date & Time API

Java 8 introduced a whole new date and time API (classes in java.time.* package) to fix the shortcomings of the legacy Date and Calendar API. The new API provides several utility methods for date and time manipulations. You can use utility classes like Period, Duration, and ChronoUnit to easily calculate the difference between two instances of new date and time API class.

LocalDate Class

The LocalDate class presents the date without time in ISO-8601 format (yyyy-MM-dd). Unlike the legacy Date class, it doesn't store any timezone information.

The following example demonstrates how you can use the Period class to calculate the number of days between two LocalDate instances:

// create date instances
LocalDate localDate1 = LocalDate.parse("2019-12-31");
LocalDate localDate2 = LocalDate.parse("2020-01-08");

// calculate difference
long days = Period.between(localDate1, localDate2).getDays();

// print days
System.out.println("Days between " + localDate1 + " and " + localDate2 + ": " + days);

Here is the output of the above code snippet:

Days between 2019-12-31 and 2020-01-08: 8

The new date and time API also provides ChronoUnit enum class to represent individual date-time units such as days, months, years, hours, seconds, etc. Each unit provides an implementation for a method called between() to calculate the amount of time between two temporal objects for that specific unit.

Let us look at the below example that calculates the days and months between two LocalDate instances using ChronoUnit:

// create date instances
LocalDate pastDate = LocalDate.of(2019, Month.AUGUST, 15);
LocalDate now = LocalDate.now();

// calculate difference
long months = ChronoUnit.MONTHS.between(pastDate, now);
long days = ChronoUnit.DAYS.between(pastDate, now);

// print days & months
System.out.println("Months between " + pastDate + " and " + now + ": " + months);
System.out.println("Days between " + pastDate + " and " + now + ": " + days);

The above code snippet will output the following:

Months between 2019-08-15 and 2020-01-10: 4
Days between 2019-08-15 and 2020-01-10: 148

LocalTime Class

A LocalTime represents time without date in ISO-8601 format (HH:mm:ss.SSS). To calculate the difference between two instances of LocalTime, you need to use the Duration class that represents the duration between two times.

Here is an example:

// create time instances
LocalTime firstTime = LocalTime.of(11, 30);
LocalTime secondTime = LocalTime.of(22, 35);

// calculate difference between times
Duration duration = Duration.between(firstTime, secondTime);

// print the difference
System.out.println("Hours between " + firstTime + " and " + secondTime + ": " + duration.toHours());
System.out.println("Minutes between " + firstTime + " and " + secondTime + ": " + duration.toMinutes());

The above example will print the following on the console:

Hours between 11:30 and 22:35: 11
Minutes between 11:30 and 22:35: 665

Alternatively, you can also use ChronoUnit to calculate the difference between two LocalDate in terms of specific time units as shown below:

// create time instances
LocalTime firstTime = LocalTime.of(10, 15, 45);
LocalTime secondTime = LocalTime.of(14, 50, 15);

// calculate difference between times
long hours = ChronoUnit.HOURS.between(firstTime, secondTime);
long minutes = ChronoUnit.MINUTES.between(firstTime, secondTime);
long seconds = ChronoUnit.SECONDS.between(firstTime, secondTime);

// print the difference
System.out.println("Hours between " + firstTime + " and " + secondTime + ": " + hours);
System.out.println("Minutes between " + firstTime + " and " + secondTime + ": " + minutes);
System.out.println("Seconds between " + firstTime + " and " + secondTime + ": " + seconds);

Here is how the output looks like:

Hours between 10:15:45 and 14:50:15: 4
Minutes between 10:15:45 and 14:50:15: 274
Seconds between 10:15:45 and 14:50:15: 16470

LocalDateTime Class

The LocalDateTime class represents both date and time without timezone in ISO-8601 format (yyyy-MM-ddTHH:mm:ss.SSS). The following example demonstrates how you can calculate the difference between two LocalDateTime instances of terms of days using Duration:

// create datetime instances
LocalDateTime dateTime1 = LocalDateTime.parse("2018-08-02T15:14");
LocalDateTime dateTime2 = LocalDateTime.parse("2019-02-14T12:45");

// calculate difference
Duration duration = Duration.between(dateTime1, dateTime2);

// print the difference
System.out.println("Days between " + dateTime1 + " and " + dateTime2 + ": " + duration.toDays());
System.out.println("Hours between " + dateTime1 + " and " + dateTime2 + ": " + duration.toHours());

The above example will output the following:

Days between 2018-08-02T15:14 and 2019-02-14T12:45: 195
Hours between 2018-08-02T15:14 and 2019-02-14T12:45: 4701

Let us look at another example that uses ChronoUnit to find out the difference between two LocalDateTime objects:

// create datetime instances
LocalDateTime pastDateTime = LocalDateTime.of(2019, Month.NOVEMBER, 12, 10, 40);
LocalDateTime now = LocalDateTime.now();

// calculate difference
long months = ChronoUnit.MONTHS.between(pastDateTime, now);
long days = ChronoUnit.DAYS.between(pastDateTime, now);

// print days & months
System.out.println("Months between " + pastDateTime + " and " + now + ": " + months);
System.out.println("Days between " + pastDateTime + " and " + now + ": " + days);

You should see the following output when you execute the above example:

Months between 2019-11-12T10:40 and 2020-01-10T18:36:48.356: 1
Days between 2019-11-12T10:40 and 2020-01-10T18:36:48.356: 59

ZonedDateTime Class

A ZonedDateTime represents a date and time with a timezone in ISO-8601 format (e.g 2019-05-15T10:15:30+01:00[Europe/Paris]). It is basically used to deal with timezone specific dates and times in Java 8 and higher.

Here is an example that shows how you can measure the difference between two ZonedDateTime instances:

// create datetime instances
ZonedDateTime dateTime1 = ZonedDateTime.parse("2019-05-15T10:15:30+01:00[Europe/Paris]");
ZonedDateTime dateTime2 = ZonedDateTime.parse("2020-01-05T12:00:33+05:00[Asia/Karachi]");

// calculate difference
Duration duration = Duration.between(dateTime1, dateTime2);

// print the difference
System.out.println("Days between " + dateTime1 + " and " + dateTime2 + ": " + duration.toDays());
System.out.println("Hours between " + dateTime1 + " and " + dateTime2 + ": " + duration.toHours());

As you can see above, we have created two ZonedDateTime instances in different timezones. The great thing about the new date and time API is that it will automatically calculate the timezone differences as shown in the below output:

Days between 2019-05-15T10:15:30+02:00[Europe/Paris] and 2020-01-05T12:00:33+05:00[Asia/Karachi]: 234
Hours between 2019-05-15T10:15:30+02:00[Europe/Paris] and 2020-01-05T12:00:33+05:00[Asia/Karachi]: 5638

The same date and time difference calculations can be performed through ChronoUnit enum class as shown below:

// create datetime instances
ZonedDateTime pastDateTime = ZonedDateTime.of(2018,2, 18, 10, 30, 0, 0, ZoneId.of("Europe/Paris"));
ZonedDateTime now = ZonedDateTime.now(ZoneId.systemDefault());

// calculate difference
long months = ChronoUnit.MONTHS.between(pastDateTime, now);
long days = ChronoUnit.DAYS.between(pastDateTime, now);

// print days & months
System.out.println("Months between " + pastDateTime + " and " + now + ": " + months);
System.out.println("Days between " + pastDateTime + " and " + now + ": " + days);

Here is the output for the above example:

Months between 2018-02-18T10:30+01:00[Europe/Paris] and 2020-01-10T18:48:38.368+05:00[Asia/Karachi]: 22
Days between 2018-02-18T10:30+01:00[Europe/Paris] and 2020-01-10T18:48:38.368+05:00[Asia/Karachi]: 691

OffsetDateTime Class

Finally, the last new date and time API class we'll discuss is OffsetDateTime. It represents a date and time with an offset from UTC/Greenwich in the ISO-8601 format (e.g. 2018-12-30T23:15:30+03:30).

The following example shows how you can measure the difference between two OffsetDateTime instances:

// create datetime instances
OffsetDateTime dateTime1 = OffsetDateTime.parse("2018-12-30T23:15:30+03:30");
OffsetDateTime dateTime2 = OffsetDateTime.parse("2019-07-17T05:15:30-05:00");

// calculate difference
Duration duration = Duration.between(dateTime1, dateTime2);

// print the difference
System.out.println("Days between " + dateTime1 + " and " + dateTime2 + ": " + duration.toDays());
System.out.println("Hours between " + dateTime1 + " and " + dateTime2 + ": " + duration.toHours());

The above example will output the following:

Days between 2018-12-30T23:15:30+03:30 and 2019-07-17T05:15:30-05:00: 198
Hours between 2018-12-30T23:15:30+03:30 and 2019-07-17T05:15:30-05:00: 4766

Another way to find out the difference between two OffsetDateTime is using the ChronoUnit enum class:

// create datetime instances
OffsetDateTime pastDateTime = OffsetDateTime.of(2019, 8, 25, 15, 40, 0, 0, ZoneOffset.of("+03:00"));
OffsetDateTime now = OffsetDateTime.now(ZoneOffset.systemDefault());

// calculate difference
long months = ChronoUnit.MONTHS.between(pastDateTime, now);
long days = ChronoUnit.DAYS.between(pastDateTime, now);

// print days & months
System.out.println("Months between " + pastDateTime + " and " + now + ": " + months);
System.out.println("Days between " + pastDateTime + " and " + now + ": " + days);

Here is the output of the above code snippet:

Months between 2019-08-25T15:40+03:00 and 2020-01-10T18:56:30.484+05:00: 4
Days between 2019-08-25T15:40+03:00 and 2020-01-10T18:56:30.484+05:00: 138

Check out Introduction to Java 8 Date and Time API tutorial for more new date and time API examples.

Legacy Date & Calendar API

Before Java 8, java.util.Date and java.util.Calendar classes were used for handling dates and times. These old classes have many flaws which were fixed with the release of the new date and time API in Java 8. Since these classes are still actively used in old projects, it is worth knowing how to calculate the difference between two Date objects.

The following example demonstrates how you can parse strings to create two instances of Date and then find the difference between them in milliseconds. Later, we convert the milliseconds into days and print the result on the console:

// date pattern
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

// parse string to date
Date dateBefore = formatter.parse("2019-12-05");
Date dateAfter = formatter.parse("2020-01-05");

// calculate difference in millis
long millis = Math.abs(dateAfter.getTime() - dateBefore.getTime());

// convert milliseconds to days
long days = TimeUnit.DAYS.convert(millis, TimeUnit.MILLISECONDS);

// print days
System.out.println("Days Between Dates: " + days);

The above program will output the following:

Days Between Dates: 31

Conclusion

In this article, we looked at different ways to calculate the difference between two dates in Java. We discussed Java 8 new date and time API classes that provide utility methods to easily find out the number of days between the given date and time instances.

The new date and time API introduced in Java 8 provides a wide range of classes that have simplified working in dates and times. These classes are thread-safe, easier-to-use, and come with a lot of utility methods for performing different date and time operations.

Read next: How to get current date and time in Java

✌️ Like this article? Follow @attacomsian on Twitter. You can also follow me on LinkedIn and DEV. Subscribe to RSS Feed.

👋 If you enjoy reading my articles and want to support me to continue creating free tutorials, Buy me a coffee (cost $5) .

Need help to launch a new product? I am available for contract work. Hire me to accomplish your business goals with engineering and design.