我编写了这段代码来比较两个时间值(以毫秒为单位)。比较的是当前时间与数据库中定义的时间。
fun check_for_lecture(complete:(Boolean) -> Unit) : Int{
for (x in UpdateScheduledLecturesService.lectures) {
try {
val start_time = x.start_time.toInt()
val end_time = x.end_time.toInt()
val timeRightnow = System.currentTimeMillis().toInt()
// CompareTo returns a negative number if it's less than other, or a positive number if it's greater than other.
//(also tried)if (timeRightnow == start_time || timeRightnow > start_time && timeRightnow < end_time) {
val compareWstarttime= timeRightnow.compareTo(start_time)
val compareWendtime = timeRightnow.compareTo(end_time)
if(compareWstarttime > 0 && compareWendtime < 0){
count++
lectureID = x.lectureID
Log.d(TAG, "Test: lectureId assigned")
complete(true)
} else {
Log.d(TAG, "no lectures at this time")
complete(false)
}
} catch (e: ParseException) {
Log.d(TAG, "Exception")
e.printStackTrace()
complete(false)
}
}
但是,它不起作用。我不确定我哪里出错了。
这是我打印它们时的示例:
当前时间:436239119,开始时间:1520845200,结束时间:1520848800。