【发布时间】:2018-08-20 10:23:23
【问题描述】:
我编写了这段代码来比较两个时间值(以毫秒为单位)。 比较的是当前时间与数据库中定义的时间。
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。
【问题讨论】:
-
将跟踪时间作为从 epoch 开始计数是笨拙的、容易出错的并且难以排除故障。而是使用 java.time 类。请参阅 Ole V.V. 的 the Answer。寻求更实用的方法。
标签: java android time kotlin datetime-comparison