问题
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