kotlin - 如何检查当前时间是在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
}

关于kotlin - 如何检查当前时间是在kotlin中的某个时间之前还是之后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60096853/

相关文章:

kotlin - 扩展函数是特定类的成员函数吗?

android - ViewModelProviders.of(FragmentActivity).get(ViewModel::class.java)要求 Activity 而不是get()调用中的viewmodel

android - kotlin 延迟初始化属性无法在 init block 中修改

kotlin - 如何在 Kotlin RecylerView 适配器中传递接口(interface)并在 Kotlin Activity 中获取回调

java - Android EffectFactory 位图

loops - 尽管 Kotlin 的 `Map` 不是 `Iterable` ,但它如何以及为什么可迭代?

java - Jna,随机无效内存访问错误

java - 运行时异常 : Unable to instantiate activity ComponentInfo . .. ClassNotFoundException

Kotlin - 针对 HashMap<String, String> 的对象类型检查显示警告

android - 使用 AppCompatButton 崩溃的 MaterialTimePicker 无法转换为 MaterialButton