JSTL Date comparison

天涯浪子 提交于 2020-07-18 09:33:38

问题


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 !


回答1:


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>



回答2:


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:


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>



回答4:


<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.




回答5:


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.



来源:https://stackoverflow.com/questions/12402634/jstl-date-comparison

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!