How to compare Dates with Current Date in JSP ?


I would like to know how to compare dates with todays date ?

Example  In the JSP -

Date today;
     String s_date_now;
     SimpleDateFormat formatter;
     formatter = new SimpleDateFormat("MM/dd/yyyy");
     today = new Date();
     s_date_now = formatter.format(today);

     out.print ( "JavaDate: " + s_date_now);

 <td> <span class="label">  </span> &nbsp;<% out.print ( x_crewing.getStartDateOfMemberInShow(s_email );%> <br> </td>  
<td> <span class="label">  </span>&nbsp; <% out.print ( x_crewing.getEndDateOfMemberInShow(s_email );%> <br> </td>

The output is

Member   Start Date EndDate
Sarah       01/10/2006    08/31/2006
Alex       01/10/2006     04/01/2006
My question is how do I modify the above code - so if todays date is within the above date range  - then just print "PRESENT"  else print the dates as above.

Please advise


Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hi, try this and let us know if u have any problems
sdesarAuthor Commented:
Hello ,
Thanks for the prompt reponse!

This is what I tried , but I am getting an error .. please advise
ie if Sarah's End date is after Todays date then I want to print PRESENT  , here's the code ..... please advise

<% if ( x_crewing.getEndDateOfMemberInShow("Sarah).after(s_date_now) == true  ) { out.print ("PRESENT"); }  %>

Awaiting a response.

sdesarAuthor Commented:
This is the error -

 cannot find symbol
symbol  : method after(java.lang.String)
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
it must be.(a double quote is missing).

<% if ( x_crewing.getEndDateOfMemberInShow("Sarah").after(s_date_now) == true  ) { out.print ("PRESENT"); }  %>

does x_crewing.getEndDateOfMemberInShow("Sarah") returns Date Object ????

Thank You.

using Date class is deprecated. But if u still wanna use it..following is the way

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy");
java.util.Date datestart = new java.util.Date();
java.util.Date dateend = new java.util.Date();
datestart = sdf.parse(x_crewing.getStartDateOfMemberInShow(s_email ));
dateend = sdf.parse(x_crewing.getEndDateOfMemberInShow(s_email ));

if(today.after(datestart) && today.before(dateend)){
out.println("NOT PRESENT");

you were getting error..becoz, the value returned from x_crew..... is not a Date but may be String.
Better use Calendar instead of Date.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sdesarAuthor Commented:
The x_crewing method returns a String.  
I was able to use the above code to get the data.
I will change it to use Calander.

Here are your excellent points.!!
Thanks Fargo.

sdesarAuthor Commented:
If there is no data then I get this error
javax.servlet.ServletException: Unparseable date: ""

Since x_crewing.getEndDateOfMemberInShow(s_email )) returns a String and the String is null.

How do i fix this error, I tried to check for null value but its not working.

Please advise.

sdesarAuthor Commented:
I figured it out.  I needed to check for null. Thanks anyways.  
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.