jsp - Getting current date in JSTL EL and doing arithmetic on it -
without using scriptlets, what's correct way doing date arithmetic in jsp?
here examples i'm trying do:
- get current year (yyyy)
- subtract current year 1 previous year (yyyy)
thanks!
use <jsp:usebean>
construct new date
. use jstl <fmt:formatdate>
year out of it. use el 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 pattern full year yyyy
, not yyyy
.
Comments
Post a Comment