1. Overview
In this tutorial, we’ll explore ways to find the difference between dates in Scala. We’ll go over three different approaches to solving this problem. Along the way, we’ll explore the benefits and differences between each of them.
2. Using LocalDate.toEpochDay
The first approach we’ll try involves getting the number of days from the epoch date (01-01-1970). Then, we’ll subtract these from each other to leave us with the number of days between the dates:
val fromDate = LocalDate.parse("2020-01-01")
val toDate = LocalDate.parse("2020-01-04")
toDate.toEpochDay - fromDate.toEpochDay
Here, we set toDate and fromDate, then get the number of days for each using LocalDate.toEpochDay before subtracting fromDate from toDate. This operation returns 3, telling us that there are three days between these dates. It’s also important to note that if our toDate were a date before the fromDate, we’d get a negative number when we do the subtraction.
3. Getting the Difference Using Period.between
In the previous technique, LocalDate.toEpochDay only returns the number of days between two dates. Alternatively, using Period.between returns the difference in the most appropriate units. For example, suppose we have a difference of six weeks between two dates. Instead of returning us 42 days, it’ll return a Period object of 1 month and 14 days. Let’s try out this function by calling Period.between and providing two dates:
val fromDate = LocalDate.parse("2020-01-01")
val toDate = LocalDate.parse("2020-02-15")
Period.between(fromDate, toDate)
Running this will return Period(years = 0, months = 1, days = 14). As we can see, this is a much more versatile and easier way of getting the difference.
4. Working With Units of Time Using ChronoUnit
If we want more control over the units used for the date difference, we can use the .between function on an instance of ChronoUnit. There are many implementations of the ChronoUnit enum from NANOS all the way up to FOREVER. This gives us plenty of options for which units we want to use for the date difference.
Using whatever unit of time we need, we call .between on the corresponding implementation of ChronoUnit and pass it the dates for which we want to know the difference:
val fromDate = LocalDate.parse("2020-01-01")
val toDate = LocalDate.parse("2021-02-15")
val daysDifference = ChronoUnit.DAYS.between(fromDate, toDate)
val monthsDifference = ChronoUnit.MONTHS.between(fromDate, toDate)
val yearsDifference = ChronoUnit.YEARS.between(fromDate, toDate)
After running this code snippet, daysDifference will be 411, monthsDifference will be 13, and yearsDifference will be 1.
As we can see, calling .between using the different implementations of ChronoUnit gives us much more control over the time units in which the difference is returned. However, it’s worth noting that if the difference doesn’t fit within a whole unit, it’ll leave out the remainder.
In our example, we can see that the difference is 1 year, 1 month, and 14 days. However, yearsDifference has left out the extra month and 14 days and monthsDifference has left out the additional 14 days.
5. Conclusion
In this tutorial, we explored three ways of determining the difference between two dates in our Scala code. First, we used LocalDate.toEpochDay as a quick way to get the number of days between two dates. Then, we saw that Period.between is a much more versatile and convenient way of getting the difference and returning it in the most appropriate units.
Finally, we learned that we can use ChronoUnit.between when we need more control over the unit of time, the difference is returned in but can accept that the value will exclude any remainder.
As always, the sample code used in this tutorial is available over on GitHub.