2011-05-10 49 views
1

我对时间“hh:mm:ss”有以下格式。 现在我有两个时间值,例如,如何在java中比较时间

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",""); 
} 

但无论我如果条件不工作。 我的问题是,如何比较两次,当他们在上述格式?

+0

你只是在举例说明什么是时代或者你说的是你如何构建日期? – 2011-05-10 14:14:59

+0

是的,这是我的时间价值。我必须对它进行比较。 – 2011-05-10 14:16:13

+0

'else'子句意味着时间相等,而不是比较失败。比较看起来没问题,所以你需要检查你是如何构造时间值的。 – trojanfoe 2011-05-10 14:19:00

回答

3

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

无论是这个问题还是两个日期相等,都会在其他地方执行该部分。

2

您可以尝试通过Date.getTime()函数进行比较,该函数在1970年1月1日后返回毫秒。