【发布时间】:2014-07-24 21:58:18
【问题描述】:
我有一个带有 getter setter 和方法的 bean 对象 testBean。我正在从数据库中检索结果并将其存储在 TreeMap 中
Map 的输出将如下所示:
{Student1 = [testBean[Dept=Science,ID=12,grade=A,Date=12-Jan-2013]]
[testBean[Dept=Science,ID=12,grade=B,Date=14-Mar-2013]]
{Student2 = [testBean[Dept=Science,ID=02,grade=A,Date=12-Jan-2013]]
[testBean[Dept=Science,ID=02,grade=A,Date=14-Mar-2013]]
我需要按降序排列输出,以便最新日期排在第一位。所以我使用比较器对日期进行排序:
public int DateCompare(Object studentObj, Object anotherStudentObj) {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
String value = ((testBean) studentObj).getDateTmTrans();
String value1 = ((testBean) anotherStudentObj).getDateTmTrans();
int retVal = 0;
try {
Date firstDate = dateFormat.parse(value);
Date secondDate = dateFormat.parse(value1);
retVal = firstDate.compareTo(secondDate);
} catch (ParseException e) {
e.printStackTrace();
}
return 0;
}
但我无法按降序对日期进行排序。是否有任何解决方案可以获得所需的输出?
欢迎提出建议
提前致谢
【问题讨论】:
标签: java sorting date datetime map