美好的一天。 我有另一個與Jtable相關的問題。 如果列內的日期(到期日)超過或等於當前日期,我想更改表格的行顏色。如何比較當前日期和Jtable中的給定日期?
我想這個代碼,但我得到一個錯誤:java.lang.NumberFormatException:對於輸入字符串: 「2012-03-15」
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
String expDateString = sdf.format(cal.getTime());
System.out.println(expDateString);
Double date = Double.parseDouble(expDateString);
Double val = Double.parseDouble(tableSummary.getModel().getValueAt(row, 6).toString());
for(int i=0; i<=tableSummary.getRowCount()-1; i++){
if(val >= date){
renderer.setBackground(red);
}
}
謝謝!
這裏有一個新的代碼:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
String expDateString = sdf.format(cal.getTime());
Date today = new Date(expDateString);
System.out.println("ang churva is " + today);
Date given = new Date(tableSummary.getModel().getValueAt(row, 6).toString());
for(int i=0; i<=tableSummary.getRowCount()-1; i++){
if(today.compareTo(given)>=0){
renderer.setBackground(red);
}
}
,但我得到這個異常:今天日期java.lang.IllegalArgumentException異常=新的日期(expDateString);
可能重複:http://stackoverflow.com/questions/2592501/compare-dates-in-java – assylias 2012-03-15 11:27:03
您應當存儲'Date'您'TableModel',在[檢索](http://stackoverflow.com/q/9716893/230513)之後及[渲染]之前儘早將其轉換(http://stackoverflow.com/q/9714110/230513) 。 – trashgod 2012-03-15 18:53:39