compare date with current time in jstl

HCarrasko picture HCarrasko · May 20, 2014 · Viewed 18.1k times · Source

Hello I'm trying compare a date with current time using jstl in a jsp file. How can I compare a date with the current time using jstl? I have this:

<jsp:useBean id="currentDate" type="java.util.Date"/>  
<fmt:formatDate var="now" value="${currentDate} pattern="yyyy-MM-dd"/>  

<c:if test="${ h.time < now }">
  <h1>print something</h1>
</c:if>

thanks in advance!

Answer

Susie picture Susie · May 20, 2014

convert the time to a date and compare it with the current date.

Conversion to date:

<jsp:useBean id="dateValue" class="java.util.Date" />
<jsp:setProperty name="dateValue" property="time" value="${h.time}" />

Comparision:

<c:if test="${dateValue le now}">
    //do something
</c:if>