Ingram
Ingram

Reputation: 674

comparing 2 columns in JTable for dates

I want to compare the dates in 2 columns of the same table.

I currently have the following code which prints the data from column 2 and 3 of the JTable but what I need to do is determine if a date falls between the 2 dates of the columns. How would I do this?

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;
}

Upvotes: 1

Views: 302

Answers (1)

trashgod
trashgod

Reputation: 205805

As shown here, ensure that your TableModel contains instances of Date. Because Date is Comparable, you can use compareTo() to determine if a date, d, falls between two dates, where d1 precedes or coincides with d2.

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

Upvotes: 2

Related Questions