2

再会。我还有另一个与 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 at Date today = new Date(expDateString);

4

5 回答 5

1

使用代码

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

在您的结果集查询中。它将返回差异。更改它可能符合您的需要。

于 2012-07-17T09:44:02.793 回答
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)));

}
于 2012-03-15T11:34:34.327 回答
0

线Double date = Double.parseDouble(expDateString);

这不起作用,因为字符串“2012-03-15”根本不是有效的双精度值。

我不明白您为什么要比较两个双精度值:

  1. 如果表中有日期,请使用 Date.after() 和 Date.before() 找出您的日期是现在之前还是之后。
  2. 如果表中有字符串,请使用 SimpleDateFormat.parse() 从中获取日期并执行第 1 点
于 2012-03-15T11:35:07.883 回答
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;
    }

请检查它的工作正常

于 2013-02-23T12:29:20.780 回答
0

您不能将日期字符串转换为双精度

Double date = Double.parseDouble(expDateString); //does not work!
于 2012-03-15T11:30:30.570 回答