zairahCS
zairahCS

Reputation: 325

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

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

Upvotes: 2

Views: 14909

Answers (5)

Vignesh Anand
Vignesh Anand

Reputation: 1

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

Upvotes: 0

Gomathi
Gomathi

Reputation: 1003

Use the code

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

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

Upvotes: 1

Jan Hruby
Jan Hruby

Reputation: 1497

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

Upvotes: 0

assylias
assylias

Reputation: 328727

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

}

Upvotes: 0

juergen d
juergen d

Reputation: 204844

You can't cast a date string in a double

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

Upvotes: 0

Related Questions