2016-04-05 104 views
1

我想比較同一表的2列中的日期。比較JTable中的2列日期

我目前有以下代碼,它打印JTable第2列和第3列中的數據,但我需要做的是確定日期是否在列的兩個日期之間。我將如何做到這一點?

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

回答

2

here所示,請確保您TableModel包含Date實例。由於DateComparable,因此您可以使用compareTo()來確定日期d是否落在兩個日期之間,其中d1先於或與d2一致。

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

我要把這一個去 – Ingram

+1

還要考慮'RowFilter',看到[這裏](http://stackoverflow.com/q/17854854/230513)。 – trashgod