首页 > 解决方案 > 如何检查当前时间是在kotlin中的某个时间之后还是之前

问题描述

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

标签: kotlin

解决方案


您可以使用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
}

推荐阅读