Search code examples
kotlindateunixtimestampunix-timestamp

Kotlin: how to get the start time of a day and end time of a day?


My input is unix seconds from epoch, which represents the date, that I need to work with, I need to convert the date to start time of a day and end time of a day of the same date, what are possible options to do so in Kotlin?


Solution

  • You could use LocalDate to easily achieve this behaviour using epoch

    val localDate = Instant.ofEpochMilli(timeInEpoch).atZone(ZoneId.systemDefault()).toLocalDate()
    val startDate = localDate.atStartOfDay() // (or) localDate.atTime(LocalTime.MIN)
    val endDate = localDate.atTime(LocalTime.MAX)