简体   繁体   中英

Greater than date in jsp template

I have this statement in a jsp form template

<% if (forms.getFlDate().equals("10/17/05")) { %>
<jsp:getProperty name="forms" property="flDate" /><br />
<% } %>

However, I need to test for a greater than ("10/17/05") date.

I have tried

<% if (forms.getFlDate().gt("10/17/05")) { %>
<jsp:getProperty name="forms" property="flDate" /><br />
<% } %>

but it will not work. This is not really my fortay, sort of pushed into it.

you can convert "10/17/05" to date object first by using SimpleDateFormat and then to compare use <% if (forms.getFlDate().after(myDate)) { %>

<%
    SimpleDateFormat fmt = new SimpleDateFormat("MM/dd/yy");
    java.util.Date myDate = fmt.parse("10/17/05");
    if (forms.getFlDate().after(myDate)) {
      // your logic here
    }

%>

If you are going to use this "10/17/05" more than once, introduce a variable earlier in your JSP:

<% Calendar earlyDate = Calendar.getInstance();
   earlyDate.set(2005, Calendar.OCTOBER, 17, 0, 0, 0); // ** ** **
   Date earliest = earlyDate.getTime();
%>

And then later...

<% if(forms.getFlDate().after(earliest)){ %>
    <jsp:doSomethingHere.../>
<% } %>

Please note this assumes your flDate field is declared as a Date rather than a String. If it is not, you should follow @S4beR's advice as well for using SimpleDateFormat to convert the String into a Date object.

** ** ** Use the constant for October because month in Calendar is zero-based. Facepalm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM