use*_*554 4 java sorting swing date jtable
我想按一列中的日期对Jtable进行排序,但只以dd-MM-yyyy格式显示日期。因此,所有条目都是相同的,只是不同的秒数不可见。
我有一个TableModel通过以下方法获取数据:
@Override
public Object getValueAt(int row, int col) {
Object[][] tableData = new Object[rowDataMap.keySet().size()][1];
int index = 0;
for (Long key : pane.nqm_messages.keySet())
{
Date date = rowDataMap.get(key);
SimpleDateFormat form = new SimpleDateFormat("dd-MM-yyyy");
String outputDate = form.format(date);
tableData[index][0] = outputDate;
index++;
}
return tableData[row][col];
}
Run Code Online (Sandbox Code Playgroud)
这是我的TableRowSorter,我要对行进行排序:
TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
List<SortKey> keys = new ArrayList<SortKey>();
SortKey sortKey;
sorter.setComparator( 0, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
//Sort dd-MM-yyyy
}
});
Run Code Online (Sandbox Code Playgroud)
如果我那样做,我就不能对它进行排序,因为字符串很明显。都一样。当我像这样直接使用日期对象时
tableData [index] [0] =日期
我不知道如何以正确的格式显示它,但是可以进行排序。
我该如何实现两者?