在JSTL EL中获取当前日期并对其进行算术 [英] Getting current date in JSTL EL and doing arithmetic on it
本文介绍了在JSTL EL中获取当前日期并对其进行算术的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面是我正在尝试做的例子:
- 获取当前年份(YYYY)
- 逐年递减前一年(YYYY)
谢谢!
解决方案
使用 < jsp:useBean>
构建新的 日期
。使用 JSTL < fmt:formatDate>
以获得一年的结果。使用 EL 对其进行减法。
<%@ taglib prefix =fmturi =http://java.sun.com/jsp/jstl/fmt%>
< jsp:useBean id =nowclass =java.util.Date/>
< fmt:formatDate var =yearvalue =$ {now}pattern =yyyy/>
< p>当前年:$ {year}< / p>
< p>上一年:$ {year - 1}< / p>
结果:
当年:2011
上一年:2010
注意模式全年是 yyyy
,而不是 YYYY
。
Without using scriptlets, what's the correct way for doing date arithmetic in JSP?
Here are examples what I'm trying to do:
- Get current year (YYYY)
- Subtract current year by one to get previous year (YYYY)
Thanks!
解决方案
Use <jsp:useBean>
to construct new Date
. Use JSTL <fmt:formatDate>
to get the year out of it. Use EL to substract it.
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<jsp:useBean id="now" class="java.util.Date" />
<fmt:formatDate var="year" value="${now}" pattern="yyyy" />
<p>Current year: ${year}</p>
<p>Previous year: ${year - 1}</p>
Result:
Current year: 2011
Previous year: 2010
Note that the pattern for full year is yyyy
, not YYYY
.
这篇关于在JSTL EL中获取当前日期并对其进行算术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文