【问题标题】:comparing 2 columns in JTable for dates比较 JTable 中的 2 列的日期
【发布时间】:2016-04-05 20:43:29
【问题描述】:

我想比较同一张表的 2 列中的日期。

我目前有以下代码打印来自JTable 的第 2 列和第 3 列的数据,但我需要做的是确定日期是否介于列的 2 个日期之间。我该怎么做?

public Object[][] betweendates (JTable table) {
    String tab ="\t";
    String newLine ="\n";
    TableModel dtm = table.getModel();
    int nRow = dtm.getRowCount(), nCol = dtm.getColumnCount();
    Object[][] tableData = new Object[nRow][nCol];
    for (int i = 0 ; i < nRow ; i++)
    {
        for (int j = 2 ; j <= 3 ; j++)
        {
            tableData[i][j] = dtm.getValueAt(i,j);
            System.out.print(tableData[i][j]);
            comparedates(tableData[i][j]);
            System.out.print("\t");
        }
        System.out.println();//create a new line
    }
    return tableData;
}

【问题讨论】:

    标签: java swing date jtable comparison


    【解决方案1】:

    如图所示here,确保您的TableModel 包含Date 的实例。因为DateComparable,所以您可以使用compareTo() 来确定日期d 是否介于两个日期之间,其中d1d2 之前或与d2 重合。

    Date d1 = (Date) tableData[i][2];
    Date d2 = (Date) tableData[i][3];
    boolean b = (d1.compareTo(d) < 1) && (d.compareTo(d2) < 1)
    

    【讨论】:

    • 我会试一试
    • 也可以考虑RowFilter,见here
    猜你喜欢
    • 1970-01-01
    • 2020-02-10
    • 1970-01-01
    • 2019-01-10
    • 1970-01-01
    • 2015-08-22
    • 1970-01-01
    • 2018-12-30
    • 1970-01-01
    相关资源
    最近更新 更多