java - java中如何比较时间

标签 java android datetime

我有以下时间格式 "hh:mm:ss"。 现在我有两个时间值,例如,

Date previous_time=20:50:55
Date current_time=19:50:55

我正试图这样比较它,

if(previous_time.after(current_date)){
  Log.i("Time Comparision","before is true");
      System.out.println("before");  
}
else if(current_time.after(previous_time)){
  Log.i("Time Comparision","after is true");
  System.out.println("after");   
}
else {
  System.out.println("nothing");
  Log.i("Time Comparision not happened","");
}

但是我的两个 if 条件都不起作用。 我的问题是,当它们采用上述格式时,如何比较两个时间?

最佳答案

在您的第一行代码中,您引用的是 current_date 而不是 current_time。

这就是问题所在,或者两个日期相等,这将执行 else 中的部分。

关于java - java中如何比较时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5951393/

相关文章:

java - 我在android模拟器中编译运行后,Android Studio生成了三个独立的app

java - Spark : StreamCorruptedException when deploying to OpenShift in cluster mode

java - 如何在不丢失位置的情况下对整数数组进行排序

Javascript 在 Android 的 React-Native WebView 中不起作用(设置了 javaScriptEnabled = true)

android - Android 上的 Facebook 单点登录 (SSO) 在激活时关闭 Intent

java - class.getResource ("."/*dot*/) 有什么特殊含义吗?

java - 如何使我的 Android 可刷卡 CardView 更像 IOS 7 邮件应用程序(滑动以显示按钮)

python - 日期时间模式在 python 中不匹配,即使 bash 识别它

javascript - 计算工作日问题,javascript

javascript - 如何将 moment js 日期时间转换为字符串,反之亦然