How to compare Dates with Current Date  in JSP ?

Posted on 2006-04-21
Last Modified: 2008-01-09

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


Question by:sdesar
    LVL 11

    Expert Comment

    hi, try this and let us know if u have any problems

    Author Comment

    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.


    Author Comment

    This is the error -

     cannot find symbol
    symbol  : method after(java.lang.String)
    LVL 23

    Expert Comment

    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.

    LVL 11

    Accepted Solution

    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.


    Author Comment

    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.


    Author Comment

    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.


    Author Comment

    I figured it out.  I needed to check for null. Thanks anyways.  

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Suggested Solutions

    The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
    Great sound, comfort and fit, excellent build quality, versatility, compatibility. These are just some of the many reasons for choosing a headset from Sennheiser.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now