2012-03-15 113 views
2

美好的一天。 我有另一個與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);

+1

可能重複:http://stackoverflow.com/questions/2592501/compare-dates-in-java – assylias 2012-03-15 11:27:03

+0

您應當存儲'Date'您'TableModel',在[檢索](http://stackoverflow.com/q/9716893/230513)之後及[渲染]之前儘早將其轉換(http://stackoverflow.com/q/9714110/230513) 。 – trashgod 2012-03-15 18:53:39

回答

0

你不能在你怎麼可以比較你日期的雙

Double date = Double.parseDouble(expDateString); //does not work! 
0

簡單的例子蒙上了日期字符串。請注意,如果JTable中的對象已經是日期,則不需要全部解析,這會讓您的生活更輕鬆。

下面的代碼的輸出是:

expiryDate=2012-03-15
tableDateOK=2012-03-12
tableDateExpired=2012-03-18
tableDateOK>expiryDate = false
tableDateExpired>expiryDate = true

代碼:

public static void main(String args[]) throws ParseException { 
    String expiryDate = "2012-03-15"; 
    String tableDateOk = "2012-03-12"; 
    String tableDateExpired = "2012-03-18"; 
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 

    System.out.println("expiryDate="+expiryDate); 
    System.out.println("tableDateOK="+tableDateOk); 
    System.out.println("tableDateExpired="+tableDateExpired); 
    System.out.println("tableDateOK>expiryDate = " + sdf.parse(tableDateOk).after(sdf.parse(expiryDate))); 
    System.out.println("tableDateExpired>expiryDate = " + sdf.parse(tableDateExpired).after(sdf.parse(expiryDate))); 

} 
0

Double date = Double.parseDouble(expDateString);

這不可能,因爲字符串 「2012-03-15」 的工作根本就不是一個有效的雙重價值。

我不明白爲什麼您要比較兩個double值:

  1. 如果您在表格日期,使用Date.after()和Date.before()找出來,不管你的約會現在是之前還是之後。
  2. ,如果你在表有字符串,請使用SimpleDateFormat.parse()從它那裏得到的日期,並做點1
1

使用在結果集查詢代碼

DATEDIFF('d',NOW(),exdate) 

。它會返回差異。改變它可能符合你的需求。

0
public String compareDate(Request request) throws ParseException { 
     Date debitDate= request.getPaymentTxn().getCrValDt(); 
     Date now = new Date(); 
     String response=""; 
     SimpleDateFormat sdfDate = new SimpleDateFormat("dd/MM/yyyy"); 
     String strCurrDate = sdfDate.format(now); 
     String strDebitDate = sdfDate.format(debitDate); 
     System.out.println("Current Date: " + strCurrDate); 
     Date currentDate = new SimpleDateFormat("dd/MM/yyyy").parse(strCurrDate); 
     Date txnDate = new SimpleDateFormat("dd/MM/yyyy").parse(strDebitDate); 
     System.out.println("C -> "+currentDate); 
     System.out.println("C -> "+txnDate); 
     if (txnDate!=null){ 
     if (currentDate.equals(txnDate)) 
     { 
      System.out.println("Valid Txn"); 
      response="valid"; 
     } 
     if (currentDate.after(txnDate)) 
     { 
      System.out.println("--> Not Valid TXN Past"); 
      response="notValid"; 
     } 
     if (currentDate.before(txnDate)){ 
      System.out.println("Future Valid TXn"); 
      response="future"; 
     } 
    } 
     return response; 
    } 

請CHK出來其做工精細

相關問題