如何检查当前时间是在kotlin中的某个时间之后还是之前

7mo*_*ood 3 kotlin

我正在尝试检查当前时间(小时和分钟)是否在某个时间之后或之前,我该如何在 kotlin 中执行此操作

Luc*_*rra 6

您可以使用该类Calendar

val currentTime = Calendar.getInstance()
val timeToMatch = Calendar.getInstance()

timeToMatch[Calendar.HOUR_OF_DAY] = hourToMatch
timeToMatch[Calendar.MINUTE] = minuteToMatch

when {
    currentTime == timeToMatch -> // the times are equals
    currentTime < timeToMatch -> // currentTime is before timeToMatch
    currentTime > timeToMatch -> // currentTime is after timeToMatch
}
Run Code Online (Sandbox Code Playgroud)