1. Overview

In this tutorial, we’ll learn how to calculate the difference between two dates in Kotlin. We’ll see how to determine the date difference in terms of years, months, days, etc.

2. Using Date-Time Package in Java SE 8 Release

The built-in Date-Time package, java.time, introduced in the Java SE 8 release, is based on the ISO calendar system as the default calendar. This package provides classes and methods to represent and manipulate date and time values.

The core classes in the Date-Time API have names such as LocalDate, LocalDateTime, ZonedDateTime, and OffsetDateTime.

A LocalDate is an immutable class representing a date in the ISO-8601 calendar system (yyyy-MM-dd) format, by default . However, this class doesn’t store time or timezone information.

The package has a class called Period that models a quantity or amount of time in terms of years, months, and days.

The DateTimeFormatter class has methods to parse from and format to date-time based on a given date-time pattern.

We’ll use the DateTimeFormatter class to parse the two dates to the LocalDate date instances. Then we’ll use Period to find the difference between the them.

Let’s look at the code to find the date difference using the java.time package classes:

val dateFormatter: DateTimeFormatter =  DateTimeFormatter.ofPattern("MM/dd/yyyy")

val from = LocalDate.parse(fromDate, dateFormatter)
val to = LocalDate.parse(toDate, dateFormatter)

val period = Period.between(from, to)
val years = period.years
val months = period.months
val days = period.days

In the above sample code, fromDate and toDate are the dates represented as String in the MM/dd/yyyy format.

Let’s look at some test assertions to see the expected output for the date difference between 02/01/2007 and 11/25/2022:

assertEquals(15, years)
assertEquals(9, months)
assertEquals(24, days)

3. Using Joda-Time External Library

The Joda-Time library was introduced to tackle the poor implementation of Java date-time prior to the SE 8 release. It was the goto date-time library for Java applications prior to Java SE 8. In fact, we’ll see similarities between Joda-Time and Java SE 8 Date-Time classes.

To include the functionality of the Joda-Time library, we need to add the following dependency from Maven Central:

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

org.joda.time.LocalDate is an immutable date-time class representing a date without a time zone.

org.joda.time.Period is an immutable time period specified in terms of a set of duration fields. Additionally, the PeriodType class defines the supported fields. So, the default period type supports years, months, weeks, days, hours, minutes, seconds, and milliseconds.

org.joda.time.format.DateTimeFormatter controls the printing and parsing of a date-time to and from a string.

Let’s see how to find the date difference using Joda-Time library:

val dateFormatter: org.joda.time.format.DateTimeFormatter? =  DateTimeFormat.forPattern("MM/dd/yyyy")

val from = org.joda.time.LocalDate.parse(fromDate, dateFormatter)
val to = org.joda.time.LocalDate.parse(toDate, dateFormatter)

val period = org.joda.time.Period(from, to)

val years = period.years
val months = period.months
val days = period.weeks * 7 + period.days

In the above code, we see that the Period class in Joda-Time library supports weeks. As a result, to calculate the number of days left after months, we get the number of weeks and convert it to days. Then, we add it to the actual number of days.

4. Conclusion

In this article, we learned how to use Java Date-Time (Java SE 8 built-in) and Joda-Time (external) libraries to calculate the difference between two given dates. Additionally, we noticed the similarities between both libraries.

As always, code snippets can be found over on GitHub.