2

我想使用比较两个日期Struts2

One is returned from the backend  (test.currentDate) == "2012-11-15)"
The other one I just set "2014-10-19"

如何比较两个日期?

我的代码如下:

<s:set name="currentDate"  value="%{test.currentDate}"/>
<s:set name="futureDate" value="2014-10-19"/>
<s:if test="%{#currentDate.before(#futureDate)}">
    <s:text name="test"/> <s:date name="test.currentDate" format="MM/yy"/> 
</s:if>
<s:else>
    <s:text name="test2"/>
</s:else>
4

2 回答 2

4

在上面,您尝试将字符串与导致问题的日期对象进行比较。我建议您编写一个帮助类来比较日期并利用 OGNL 静态方法访问。OGNLBasics-访问静态属性

    <s:set var="currentDate" value="%{new java.util.Date()}"/>
    <s:set var="futureDate" value="%{new java.util.Date()}"/>
    <s:property value="{#currentDate}"/>
    <s:property value="{#futureDate}"/>
    <s:if test="%{@com.mycompany.temp.strtus2.example.HelloWorld@compareDate(#currentDate, #futureDate)}">
        <div>Do your stuff</div>
    </s:if>
    <s:else>
        <div>else condition</div>
    </s:else>

实用类

public static boolean compareDate(Date currentDate, Date futureDate) {
    boolean flag = false;
    if (removeTime(currentDate).equals(removeTime(futureDate))) {
        flag = true;
    } 
    return flag;
}

public static Date removeTime(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    return cal.getTime();
}
于 2012-11-15T06:09:50.603 回答
1

futureDate然后在 JSP 中为您的 , (例如 2014-10-19 == 1413706569870)获取时间戳长值

<s:if test="1413706569870l > test.currentDate.time">

或者

<s:if test="test.currentDate.before(new java.util.Date(1413706569870l))">
于 2012-11-14T19:32:27.147 回答