Comparing dates using struts 2 in JSP(在 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 比较日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 JSP 中使用 struts 2 比较日期


- Jersey REST 客户端:发布多部分数据 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01