How to Compare the Current Date and a given Date in a Jtable?

跟風遠走 提交于 2019-12-12 18:08:49

问题


Good Day. I've got another problem related to Jtable. I want to change the row color of a table if the date inside column (expiry) exceeds or is equal to the current date.

I tried this code but i get an error: java.lang.NumberFormatException: For input string: "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);
   }
}

Thanks!

here's a new code:

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

but i get this exception: java.lang.IllegalArgumentException at Date today = new Date(expDateString);


回答1:


Use the code

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

in your resultset query. It will return the difference. Alter it possibly to match your needs.




回答2:


You can't cast a date string in a double

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



回答3:


Simple example of how you can compare you dates. Note that if the objects in your JTable already are Dates, you don't need all the parsing, which would make your life easier.

The output of the code below is:

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

Code:

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

}



回答4:


line Double date = Double.parseDouble(expDateString);

this cannot work because string "2012-03-15" is simply not a valid double value.

I do not understand why you are trying to compare two double values:

  1. if you have Date in table, use Date.after() and Date.before() to find out, whether your date is before or after now.
  2. if you have String in table, use the SimpleDateFormat.parse() to get Date from it and do point 1



回答5:


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

PLease Chk it out its working fine



来源:https://stackoverflow.com/questions/9718691/how-to-compare-the-current-date-and-a-given-date-in-a-jtable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!