在 JSP 中使用 struts 2 比较日期 [英] Comparing dates using struts 2 in JSP

查看:25
本文介绍了在 JSP 中使用 struts 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>

推荐答案

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

In the above you were trying compare string with date object that's causing the problem. I suggest you to write a helper class to compare date and take the advantage of OGNL static method access. OGNLBasics-Accessingstaticproperties

    <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();
}

这篇关于在 JSP 中使用 struts 2 比较日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆