如何比较两个不同的时间戳?

时间:2018-03-12 11:49:46

标签: java android time kotlin datetime-comparison

我已编写此代码以比较两个时间值(以毫秒为单位)。 比较是当前时间与数据库中定义的时间。

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。

3 个答案:

答案 0 :(得分:2)

System.currentTimeMillis()会返回long值,转换为int后,您的精确度就会降低。

示例:我现在已经运行了代码,System.currentTimeMillis()返回1520856967725。超过1.5 万亿,这太大而不适合intint的最大值是2147483647 - 大约2.1的十亿)。

当您转换为int时,它只获得最后32位,而且该值变为438544942

10110001000011010001000111010101000101101 - 1520856967725
         00011010001000111010101000101101 - 438544942 (last 32 bits)

要使代码生效,请将开始和结束时间转换为long,因为这是正确的类型,以适合您要比​​较的值。并且不要将currentTimeMillis转换为int,因为它是long值。

PS:另一个细节是currentTimeMillis返回自unix epoch以来毫秒的数量。但是某些API还可以使用的数量。确保在比较之前您的开始和结束时间返回的值(根据您的值,似乎开始和结束时间以秒为单位 - 如果那是&#39;案例,在将currentTimeMillis转换为任何内容之前,您应该将# port install php71-zip 除以1000。

答案 1 :(得分:1)

问题在于您的开始时间和结束时间是,因为纪元,而System.currentTimeMillis()(如名称所示)为您提供毫秒从那个时代开始。所以这些价值不容易比较。

平庸的解决方案是在比较之前将前两个乘以或将第三个除以1000。但是,对于试图阅读和理解代码的下一个程序员来说,对日期时间值的这种算术运算并不是非常友好。

相反,我建议您使用库类进行转换和比较。来自java.time的Instant是现代Java日期和时间API,是您需要的。对不起,我不能写Kotlin代码,但我相信从Java翻译不会太难:

    long start_time = x.getStart_time();
    long end_time = x.getEnd_time();
    Instant timeRightnow = Instant.now();
    if (timeRightnow.isAfter(Instant.ofEpochSecond(start_time)) 
            && timeRightnow.isBefore(Instant.ofEpochSecond(end_time))) {
        // do something
    }

更好的是,如果您可以在数据库中使用datetime数据类型,而不是存储自纪元以来的秒数。这将使查询到数据库的输出更具可读性,并且通常有助于调试。并消除你的问题。

问题:我可以在Android上使用java.time吗?

是的,您可以在Android上使用java.time。它只需要至少Java 6

  • 在Java 8及更高版本和更新的Android设备上,内置了现代API。
  • 在Java 6和7中获取ThreeTen Backport,新类的后端端口(适用于JSR 310的ThreeTen;请参阅底部的链接)。
  • On(较旧)Android使用Android版的ThreeTen Backport。它被称为ThreeTenABP。并确保使用子包从org.threeten.bp导入日期和时间类。

链接

答案 2 :(得分:0)

你可以使用joda apis。例如下面的

import org.joda.time.DateTime;

public class CompareDateTimes {

  public static void main(String[] args) {

    // dt1 and dt2 have the same date and time
    DateTime dt1 = new DateTime();
    DateTime dt2 = new DateTime(dt1);

    // dt3 is one day later
    DateTime dt3 = new DateTime(dt2.plusDays(1));

    System.out.println("dt1.equals(dt2): " + dt1.equals(dt2));
    System.out.println("dt1.equals(dt3): " + dt1.equals(dt3));

    System.out.println("dt1.isAfter(dt3): " + dt1.isAfter(dt3));
    System.out.println("dt1.isBefore(dt3): " + dt1.isBefore(dt3));
  }
}