1. Overview

Calculating the difference between two time periods is a common task when dealing with date and time-related operations. We can find it useful while determining the duration between two events or tracking time intervals.

In this tutorial, we’ll learn different ways to calculate the time difference between two time periods in Kotlin.

2. Using kotlin.time.Duration

We can use the kotlin.time.Duration class from the Kotlin standard library to represent a time period in seconds, minutes, hours, and so on.

Let’s initialize two time periods, namely, startDuration and endDuration as Duration objects:

val startDuration = 1.days + 12.hours + 15.minutes
val endDuration = 8.hours + 45.minutes

Now, we can conveniently find the difference between the two time periods using the minus() method:

val difference: Duration = startDuration.minus(endDuration)

Lastly, let’s verify that difference has got the correct time duration:

assertEquals(1.days + 3.hours + 30.minutes, difference)

Great! It looks like we’ve got this one right.

3. Using kotlinx.datetime.Instant

We can use the kotlinx.datetime.Instant class from the kotlinx-datetime library to represent an exact point in time in UTC. In this section, let’s learn how to measure the difference between two time periods represented as epoch timestamps.

3.1. Dependency Setup

To use the kotlinx-datetime library, we must add the dependency to kotlinx-datetime-jvm in the project’s pom.xml:

<dependency>
    <groupId>org.jetbrains.kotlinx</groupId>
    <artifactId>kotlinx-datetime-jvm</artifactId>
    <version>0.4.0</version>
</dependency>

That’s it. We can now use this library to solve our use case.

3.2. Finding Difference

First, let’s define the startEpochMillis and endEpochMillis timestamps:

val startEpochMillis = 1700617592000L
val endEpochMillis = 1700617692000L

We can use one of the epoch converter utilities to see that these represent “Wednesday, November 22, 2023 1:46:32 AM” and “Wednesday, November 22, 2023 1:48:12 AM” in UTC.

Next, let’s create two objects of the Instant class, namely, instant1 and instant2 from startEpochMillis and endEpochMillis:

val instant1 = Instant.fromEpochMilliseconds(startEpochMillis)
val instant2 = Instant.fromEpochMilliseconds(endEpochMillis)

Now, we can determine the difference between these two events using the minus() method:

val durationDiff = instant2.minus(instant1)

Finally, let’s verify that our approach works correctly:

assertEquals(1.minutes + 40.seconds, durationDiff)

It works as expected.

4. Using kotlin.time.TimeSource

One of the popular use cases for calculating time difference is to measure the time taken by an operation. We can use the TimeSource API to find the time difference for such scenarios.

First, let’s initialize the timeSource object with the TimeSource.Monotonic implementation for TimeSource:

val timeSource = TimeSource.Monotonic

Now, we can use the markNow() method to mark a point, mark1, on the timeline provided by timeSource:

val mark1 = timeSource.markNow()

Further, we can call the operation for which we’re doing the time measurement and then mark a new point, mark2 on the timeline:

// call operation
val mark2 = timeSource.markNow()

Finally, we can compute the time difference by subtracting the two values:

val difference = mark2 - mark1

That’s it. We get the time difference as a Duration object in the difference object.

5. Conclusion

In this article, we learned how to find the time difference between two time periods in Kotlin. Furthermore, we explored the Kotlin.time.Duration, Kotlinx.datetime.Instant, and kotlin.time.TimeSource classes to solve the use case of measuring time difference.

As always, the code from this article is available over on GitHub.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.