我怎样才能在两个日期之间创建一个 TableRowShorter

How can i make a TableRowShorter between two Date

combobox.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent arg0) {
                if(arg0.getStateChange()==ItemEvent.SELECTED) {
                    if(!combobox.getSelectedItem().toString().equals(items[0])) { //items[] for the items of the combobox.
                        DefaultTableModel table1 = (DefaultTableModel)table.getModel();
                        String search =combobox.getSelectedItem().toString();
                        TableRowSorter<DefaultTableModel> tr = new TableRowSorter<DefaultTableModel>(table1);
                        table.setRowSorter(tr);
                        tr.setRowFilter(RowFilter.regexFilter(search));
                    }
                }
            }
        });

我用这个代码。当我 select 组合框的一个项目时,它会用 selected 项目对 table 进行排序。我有第二个组合框。假设组合框的名称是 combobox2,combobox2 的项目是“最近 2 个月”。

table 的第一列是日期。当我 select combobox2(过去 2 个月)的项目时,我想对 table 进行排序。我只想查看最多 2 个月前的行。

例如,如果我发送字符串 ("01/01/2020"),我只想查看包含 ("01/01/2020") 和此日期之后的行。

但是使用此代码我只能看到我发送的日期。我希望我解释得很好。如果你们需要,我可以分享更多代码。

For example if I sent the String ("01/01/2020"), I only want to see the rows with ("01/01/2020") and after this date.

您需要使用“日期过滤器”。

例如,此过滤器将 return 特定日期之后的所有日期:

RowFilter.dateFilter(ComparisonType.AFTER, new Date());

阅读 RowFilter API 了解您可以使用的其他过滤器。

注:

您不应在模型中存储字符串值。您应该将日期存储在表示日期的对象中,然后使用自定义渲染器来格式化日期。这将使上面的过滤器正常工作。