11

I've seen some posts about date comparisons in JSTL, but I still can't get it to work.

I have a date field, which I want to test whether is after 01-01-1970.

<c:if test="${user.BirthDate > whatGoesHere}">
    <doSomeLogic/>
</c:if>

Maybe a bean should be used?

Thanks !

5 Answers 5

20

Just use <fmt:formatDate> to extract the year from it so that you can do the math on it.

<fmt:formatDate value="${user.birthDate}" pattern="yyyy" var="userBirthYear" />
<c:if test="${userBirthYear le 1970}">
    <doSomeLogic/>
</c:if>
1
  • 2
    Comparing the year of a Date is Avery limited use case. If you want to compare two Dates down to the millisecond, use built in Comparator of Date. Saurabh Ande answered it correctly below. This also doesn't answer the problem, it appears the OP needs it to at least day of month precision. Commented Sep 13, 2018 at 19:01
9

U can use jstl and usebean something like this

    <jsp:useBean id="now" class="java.util.Date"/>
    <c:if test="${someEvent.startDate lt now}"> 
    It's history!</c:if>
1
  • is there a bean for Unix Date(0) ? (01/01/1970)
    – Nati
    Commented Sep 13, 2012 at 10:41
6

You could also add a boolean getter to you bean:

public boolean isBirthDateAfter1970(){
    return birthDate.after(dateOf1970);
}

So you can use the following EL:

<c:if test="${user.birthDateAfter1970}">
   You were born after the sixties.
</c:if>
3
<c:if test="${date1.time > date2.time}">...</c:if>

or lt,=,gt:

<c:if test="${date1.time gt date2.time}">...</c:if>

We exploit Long getTime() method of java.util.Date.

0

You can use the (deprecated) methods getYear(), getMonth(), etc.

<c:if test="${user.BirthDate.year > 99}">
  <doSomeLogic/>
</c:if>

Note that getYear() returns the year number - 1900.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.