JSTL日期比较

时间:2012-09-13 08:49:37

标签: jstl jsp-tags java.util.date

我在JSTL上看过一些关于日期比较的帖子,但我仍然无法让它发挥作用。

我有一个日期字段,我想测试是否在01-01-1970之后。

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

}

也许应该使用bean?

谢谢!

5 个答案:

答案 0 :(得分:18)

只需使用<fmt:formatDate>从中提取年份,以便您可以对其进行数学计算。

<fmt:formatDate value="${user.birthDate}" pattern="yyyy" var="userBirthYear" />
<c:if test="${userBirthYear le 1970}">
    <doSomeLogic/>
</c:if>

答案 1 :(得分:6)

你可以使用jstl和usebean这样的东西

    <jsp:useBean id="now" class="java.util.Date"/>
    <c:if test="${someEvent.startDate lt now}"> 
    It's history!</c:if>

答案 2 :(得分:6)

你也可以给你的bean添加一个布尔的getter:

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

所以你可以使用以下EL:

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

答案 3 :(得分:3)

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

lt=gt

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

我们利用Long getTime()的{​​{1}}方法。

答案 4 :(得分:0)

您可以使用(已弃用)方法getYear()getMonth()等。

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

请注意getYear()会返回年份编号 - 1900。