比较自定义类列表android中的两个时间戳

时间:2014-04-08 10:48:29

标签: java android

我想比较使用比较器的两个时间戳,例如..我需要在我的对象列表中比较这两次08:30 AM和11:00 PM,以便按时间排序对象。

任何人都可以建议我,我将如何完成这项任务。

谢谢!

2 个答案:

答案 0 :(得分:0)

public int compare(Object obj1, Object obj2)
{
 // extract time i.e,  08:30 / 11:00 and AM / PM indicator from your timestamp. a simple split() could do it.
 // now if indicator1 > indicator2 return 1
// if indicator1 < indicator2 return -1
// if indicator1 == indicator2, convert time1 and time2 to minutes (lazy conversion) from 00:00 and return 1 if intTime1>intTime2. -1 otherwise
// use a ternery operator for the above if you eant to.
}

答案 1 :(得分:0)

时间戳只是一个Long值,表示自Epoch(1970年1月1日)以来的秒数。您可以像使用任何其他号码一样使用它们。

因此,要构建一个返回通常的负0正值的比较器,只需执行以下操作:

public int compare(Object val1, Object val2) {
    return (Long) val1 - (Long) val2;
}