Kotlin Program to Calculate Difference Between Two Time Periods

Calculating the difference between two time periods is a common task in many applications. In this article, we will explore how to achieve this using Kotlin. We will provide three different Kotlin Program to Calculate Difference Between Two Time Periods, each with a unique approach and explanation. By the end of this article, you will have a solid understanding of how to calculate time differences in Kotlin.

1. Introduction

In Kotlin, calculating the difference between two time periods can be done in various ways. We can create custom classes, utilize Kotlin’s built-in LocalTime class, or handle more complex scenarios involving dates and times. Each method has its own advantages and use cases.

2. Example 1: Basic Time Difference Calculation

2.1 Program Explanation

In this example, we will create a simple Time class with three member variables: hours, minutes, and seconds. We will then write a function to calculate the difference between two Time objects.

2.2 Code Implementation

Kotlin
class Time(internal var hours: Int, internal var minutes: Int, internal var seconds: Int)

fun main(args: Array<String>) {
    val start = Time(12, 34, 55)
    val stop = Time(8, 12, 15)
    val diff: Time

    diff = difference(start, stop)

    println("TIME DIFFERENCE: ${start.hours}:${start.minutes}:${start.seconds} - " +
            "${stop.hours}:${stop.minutes}:${stop.seconds} = " +
            "${diff.hours}:${diff.minutes}:${diff.seconds}")
}

fun difference(start: Time, stop: Time): Time {
    val diff = Time(0, 0, 0)

    if (stop.seconds > start.seconds) {
        --start.minutes
        start.seconds += 60
    }

    diff.seconds = start.seconds - stop.seconds
    if (stop.minutes > start.minutes) {
        --start.hours
        start.minutes += 60
    }

    diff.minutes = start.minutes - stop.minutes
    diff.hours = start.hours - stop.hours

    return diff
}

2.3 Output

Kotlin
TIME DIFFERENCE: 12:34:55 - 8:12:15 = 4:22:40

3. Example 2: Using Kotlin’s LocalTime Class

3.1 Program Explanation

In this example, we will use Kotlin’s built-in LocalTime class from the java.time package to calculate the difference between two time periods. This approach is more robust and leverages Kotlin’s standard library.

3.2 Code Implementation

Kotlin
import java.time.LocalTime
import java.time.Duration

fun main(args: Array<String>) {
    val start = LocalTime.of(12, 34, 55)
    val stop = LocalTime.of(8, 12, 15)

    val duration = Duration.between(stop, start)

    val hours = duration.toHours()
    val minutes = duration.toMinutes() % 60
    val seconds = duration.seconds % 60

    println("TIME DIFFERENCE: $hours hours, $minutes minutes, $seconds seconds")
}

3.3 Output

Kotlin
TIME DIFFERENCE: 4 hours, 22 minutes, 40 seconds

4. Example 3: Handling Time Periods with Dates

4.1 Program Explanation

This example extends the previous one by including date information. We will use LocalDateTime to calculate the difference between two date-time periods. This method is useful for applications that need to handle both date and time.

4.2 Code Implementation

Kotlin
import java.time.LocalDateTime
import java.time.Duration

fun main(args: Array<String>) {
    val start = LocalDateTime.of(2023, 5, 20, 12, 34, 55)
    val stop = LocalDateTime.of(2023, 5, 20, 8, 12, 15)

    val duration = Duration.between(stop, start)

    val days = duration.toDays()
    val hours = duration.toHours() % 24
    val minutes = duration.toMinutes() % 60
    val seconds = duration.seconds % 60

    println("TIME DIFFERENCE: $days days, $hours hours, $minutes minutes, $seconds seconds")
}

4.3 Output

Kotlin
TIME DIFFERENCE: 0 days, 4 hours, 22 minutes, 40 seconds

Conclusion

In this article, we explored three different ways to calculate the difference between two time periods in Kotlin. The first example showed a basic method using a custom Time class. The second example leveraged Kotlin’s LocalTime class for a more robust solution. The third example extended the concept to include dates using LocalDateTime. Each approach has its own use cases and can be chosen based on the requirements of your application.