Difference Between Dates in Java

Date calculations is a very common use case in Java.In this post, we will learn the different options to calculate the difference between dates in Java.

 

1. Core Java Options

Java 8 introduced a date and time API.We are covering following options for calculating the difference between two dates.

  • Calculation using Java 8 date time API.
  • Using java.util.Date

 

1.1 Java 8 

Java 8 introduced a Date Time API.This new API makes it easy to calculate the difference in more flexible and intuitive way.We use LocalDate, LocalDateTime and Duration classes to represent date and time in the new API.

public class Java8DateCalculation {

 public void calculate_difference_between_dates() {

  //Use LocalDate and ChronoUnit
  LocalDate firstDate = LocalDate.of(2017, 5, 6);
  LocalDate currentDate = LocalDate.now();

  long days = ChronoUnit.DAYS.between(firstDate, currentDate);
  System.out.println(days);

  //date calculation
  LocalDate now = LocalDate.now();
  LocalDate tenDaysAhead = now.plusDays(10);
  System.out.println(tenDaysAhead.toString());
 }
}

 

1.2. Using java.util.Date

public class DateDifferenceExample {

 public void calculate_difference_between_dates() throws ParseException {
  String currentDate = "10/24/2017";
  String finalDate = "10/28/2017";

  SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH);
  Date firstDate = simpleDateFormat.parse("06/24/2017");
  Date secondDate = simpleDateFormat.parse("06/30/2017");

  long difference = Math.abs(firstDate.getTime() - secondDate.getTime());
  long differenceDates = difference / (24 * 60 * 60 * 1000);

  //Convert long to String
  String dayDifference = Long.toString(differenceDates);
  System.out.println("Day Differnec is " + dayDifference);
 }
}

 

2. External Libraries

Java 8 provides first-class support for the Date and Time API which address most of the issue exists in the pre-Java 8 API’s.We still have some options to use better date and time API even if you are working on the legacy application with no support for Java 8.

 

2.1 The Joda-Time

Joda-Time is the best alternate option while working on calculating the difference between two dates in Java.Java 8 date and time API is inspired by the Joda-Time API and carry many features available in this library.

DateTime now = DateTime.now();
Period period = new Period( now, now.plusHours( 4 ).plusMinutes( 30));
System.out.println( "period: " + period );

Interval interval = new Interval(oldTime, new Instant());

To include Joda-Time library in your application, add the following dependency in your pom.xml file.If this is a non-maven project, add required jar files in your project classpath.

<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.9.9</version>
</dependency>

[pullquote align=”normal”]The Joda-Time project is now in maintenance mode, with the team advising migration to the java.time classes [/pullquote]

 

As the other alternative, we can also consider Date4J as a good third-party replacement.

 

Summary

In this quick post, we learn the different options to calculate the difference between dates in Java.We discussed the option to do these calculations before Java 8 and with Java 8. We also explored the possibility to use Joda-Time API for the calculations.