Sling Academy
Home/Kotlin/Finding the Start and End of a Day in Kotlin

Finding the Start and End of a Day in Kotlin

Last updated: December 04, 2024

When working with dates and times in Kotlin, there are scenarios where you might need to determine the start and end of any given day. This is particularly useful when data needs to be grouped by days or when conducting daily reports. In this article, we'll explore how to achieve this using Kotlin and its standard libraries along with additional support from libraries like java.time, available in Java 8 and above.

Understanding Dates and Times in Kotlin

Kotlin itself doesn’t include any date or time handling classes in its own standard library. Instead, it relies on Java's robust java.time package which was introduced in Java 8 as part of the new Date & Time API. This package is designed to overcome the inconsistencies and shortcomings of the old Date and Calendar classes.

The central class we will work with here is LocalDateTime. This class represents a date-time without a time zone in the ISO-8601 calendar system.

Setting up Your Environment

First, make sure that your Kotlin project is set up to use Java 8 or above. Typically, this involves specifying the Java version in your build files. For a Gradle-based project, your build.gradle.kts file should include:


targetCompatibility = JavaVersion.VERSION_1_8

Finding the Start of the Day

To find the start of a day in Kotlin using LocalDateTime, you need to select the desired date and then adjust it to the start of that particular day, typically 00:00. Here's how you can do it:


import java.time.LocalDate

fun getStartOfDay(date: LocalDate): LocalDateTime {
    return date.atStartOfDay()
}

fun main() {
    val today = LocalDate.now()
    val startOfToday = getStartOfDay(today)
    println("Start of today: ", startOfToday)
}

In this example, atStartOfDay() is a built-in function of LocalDate that converts the date to a LocalDateTime instance at the start of the date, i.e., midnight.

Finding the End of the Day

The end of a day is typically defined as the last moment of the day, just before the next day begins. This would be 23:59:59 on the 24-hour clock. To get this in Kotlin, you perform a slightly more complex operation, since there’s no direct method like atStartOfDay().


import java.time.LocalDateTime
import java.time.LocalTime

fun getEndOfDay(date: LocalDate): LocalDateTime {
    return LocalDateTime.of(date, LocalTime.MAX)
}

fun main() {
    val today = LocalDate.now()
    val endOfToday = getEndOfDay(today)
    println("End of today: ", endOfToday)
}

In this code, LocalTime.MAX is used, which represents the latest time of 23:59:59.999999999.

Utility Function for a Given Date

Based on these two functions, you can create a utility function that accepts any LocalDate and retrieves both start and end:


data class DayBounds(val start: LocalDateTime, val end: LocalDateTime)

fun getDayBounds(date: LocalDate): DayBounds {
    val start = getStartOfDay(date)
    val end = getEndOfDay(date)
    return DayBounds(start, end)
}

fun main() {
    val date = LocalDate.of(2023, 10, 15)
    val bounds = getDayBounds(date)
    println("Start of the day: " + bounds.start)
    println("End of the day: " + bounds.end)
}

This makes it easy to handle any date without hardcoding logic in your application’s main method. Such utility functions can enhance the reusability and cleanliness of your code.

Common Use Cases

Finding the start and end of a day has numerous applications. For instance, it can help in creating time intervals for applications that rely on temporal operations, such as scheduling software, data analysis over specific periods, or time-series data handling in financial applications.

Conclusion

The Java Date and Time API coupled with Kotlin's concise syntax offers a powerful combination for handling date-time operations. By leveraging LocalDateTime methods such as atStartOfDay() and using constants like LocalTime.MAX, you can efficiently determine the start and end of any given day. These techniques improve code readability and reliability when working with time-based data within your Kotlin applications.

Next Article: How to Truncate Time to Remove Seconds or Milliseconds in Kotlin

Previous Article: Sorting a List of Dates in Kotlin

Series: Working with date & time in Kotlin

Kotlin

You May Also Like

  • How to Use Modulo for Cyclic Arithmetic in Kotlin
  • Kotlin: Infinite Loop Detected in Code
  • Fixing Kotlin Error: Index Out of Bounds in List Access
  • Setting Up JDBC in a Kotlin Application
  • Creating a File Explorer App with Kotlin
  • How to Work with APIs in Kotlin
  • What is the `when` Expression in Kotlin?
  • Writing a Script to Rename Multiple Files Programmatically in Kotlin
  • Using Safe Calls (`?.`) to Avoid NullPointerExceptions in Kotlin
  • Chaining Safe Calls for Complex Operations in Kotlin
  • Using the Elvis Operator for Default Values in Kotlin
  • Combining Safe Calls and the Elvis Operator in Kotlin
  • When to Avoid the Null Assertion Operator (`!!`) in Kotlin
  • How to Check for Null Values with `if` Statements in Kotlin
  • Using `let` with Nullable Variables for Scoped Operations in Kotlin
  • Kotlin: How to Handle Nulls in Function Parameters
  • Returning Nullable Values from Functions in Kotlin
  • Safely Accessing Properties of Nullable Objects in Kotlin
  • How to Use `is` for Nullable Type Checking in Kotlin