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

5

2

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

Lactate answered 15/3, 2012 at 11:23 Comment(2)
possible duplicate: https://mcmap.net/q/25560/-how-to-compare-dates-in-java-duplicateIsometric
You should be storing a Date in your TableModel, converting it at the earliest opportunity after retrieval and well before rendering.Amii
P
1

Use the code

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

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

Phi answered 17/7, 2012 at 9:44 Comment(0)
G
0

You can't cast a date string in a double

Double date = Double.parseDouble(expDateString); //does not work!
Girosol answered 15/3, 2012 at 11:30 Comment(0)
I
0

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

}
Isometric answered 15/3, 2012 at 11:34 Comment(0)
J
0

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
Jaconet answered 15/3, 2012 at 11:35 Comment(0)
H
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;
    }

PLease Chk it out its working fine

Heisenberg answered 23/2, 2013 at 12:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.