I would like to get a list of zoneddatetimes by ho...
# announcements
v
I would like to get a list of zoneddatetimes by hour between 2 zoneddatetimes
Copy code
val startTime = Instant.ofEpochMilli(1589778000000).atZone(ZoneOffset.UTC) // May 18 @ 05:00 UTC
val endTime = Instant.ofEpochMilli(1589950800000).atZone(ZoneOffset.UTC) // May 20 @ 05:00 UTC

val res: List<ZonedDateTime> = startTime.hoursUntil(endTime)

//[May 19 05:00, May 19 06:00 ...]
So that I can do a group by day on it
Copy code
val final = res.groupBy{it.day}.toMap()
{ May 18 -> 19 hours , May 19: 24 hours, May 20: 5 hours}
Is this possible?
m
you could eg generate a sequence of dates with
generateSequence
, starting at the current date and incrementing by `step`:
Copy code
fun ZonedDateTime.until(
    end: ZonedDateTime,
    step: Duration = Duration.ofHours(1)
) = generateSequence(this) {
    (it + step).takeIf { it < end }
}
2
v
neat thanks