Comparing dates from database

R

ruds

Hi,
I want to compare 3 dates from my database. Actually, I want to check
one date with the other 2 dates if they are befor or after the first
date. I have tried using the before and after methods but I'm not
getting any o/p.
Here is the code for comparing: fdate, cdate & accdate are the dates
from database retrived using rs.getDate();

<%f((ifdate.equals("null")) && (cdate.equals("null"))) { %> NA <%}
else{ if((fdate.after(accdate)) || (cdate.after(accdate))){%> N <%}
else if((fdate.equals("null")) || (cdate.after(accdate))) {%> N <%}
else if((fdate.after(accdate)) || (cdate.equals("null"))) {%> N <%}
else{%> Y <%}}%>


This code is from a JSP page.
Please tell what is going wrong.

Thanks
 
L

Lew

Hi,
I want to compare 3 dates from my database. Actually, I want to check
one date with the other 2 dates if they are befor or after the first
date. I have tried using the before and after methods but I'm not
getting any o/p.
Here is the code for comparing: fdate, cdate& accdate are the dates
from database retrived using rs.getDate();

<%f((ifdate.equals("null"))&& (cdate.equals("null"))) { %> NA<%}
else{ if((fdate.after(accdate)) || (cdate.after(accdate))){%> N<%}
else if((fdate.equals("null")) || (cdate.after(accdate))) {%> N<%}
else if((fdate.after(accdate)) || (cdate.equals("null"))) {%> N<%}
else{%> Y<%}}%>


This code is from a JSP page.
Please tell what is going wrong.

First, there's Java scriptlet in your JSP. Bad.

Second, you didn't copy and paste your code: "f((ifdate.equals("null"))" is
clearly wrong.

Third, you don't show the type of 'fdate' - you compare it to a 'String' but
you use a 'Date#after()' method on it. Which is it? You can't have it both ways.

Finally, learn to format your code for readability, especially on Usenet posts.
 
R

ruds

On 09/24/2010 02:24 AM, ruds wrote:





First, there's Java scriptlet in your JSP.  Bad.

Second, you didn't copy and paste your code: "f((ifdate.equals("null"))" is
clearly wrong.

Third, you don't show the type of 'fdate' - you compare it to a 'String' but
you use a 'Date#after()' method on it.  Which is it?  You can't have it both ways.

Finally, learn to format your code for readability, especially on Usenet posts.

Sorry. Here's my copy pasted code:

<% if((fdate.equals(null))&&(cdate.equals(null))){%>NA <%}
else{ if((fdate.after(accdate)) || (cdate.after(accdate))){%> N <%}
else if((fdate.equals(null))||(cdate.after(accdate))){%> N <%}
else if((fdate.after(accdate))||(cdate.equals(null))){%> N <%}
else{%> Y <%}}%>

Here accdate,fdate & cdate are the java.sql.Date objects. If fdate or
accdate is not present it has null value. accdateis always present and
it is the one which should be compared against.
 
R

ruds

If i just compare fdate with accdate the data gets displayed properly.

if(ifdate.after(accdate)){ %> N <%}else{%> Y <%}%>

thanks.
 
L

Lew

ruds said:
Sorry. Here's my copy pasted code:

<% if((fdate.equals(null))&&(cdate.equals(null))){%>NA<%}

Too many parentheses, and why did you ignore my remark about readability?

'if((fdate.equals(null))' can never evaluate to 'true'. It will either be
'false' or throw an exception. If it throws an exception, it will cause the
application screen to do whatever it is that it did, which you did not describe.
else{ if((fdate.after(accdate)) || (cdate.after(accdate))){%> N<%}
else if((fdate.equals(null))||(cdate.after(accdate))){%> N<%}
else if((fdate.after(accdate))||(cdate.equals(null))){%> N<%}
else{%> Y<%}}%>

Here accdate,fdate& cdate are the java.sql.Date objects. If fdate or
accdate is not present it has null value. accdateis always present and
it is the one which should be compared against.

"If ... accdate is not present ... accdate is always present ... "?

If either 'fdate' or 'cdate' is ever 'null', your code will blow up with a
'NullPointerException'. You cannot dereference the pointer in
'fdate.equals(...)' when 'fdate == null'.
 
R

Roedy Green

<% if((fdate.equals(null))&&(cdate.equals(null))){%>NA <%}
else{ if((fdate.after(accdate)) || (cdate.after(accdate))){%> N <%}
else if((fdate.equals(null))||(cdate.after(accdate))){%> N <%}
else if((fdate.after(accdate))||(cdate.equals(null))){%> N <%}
else{%> Y <%}}%>

Another approach is to do the compares in SQL/procedure language and
just return the final boolean.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top