Search code examples
jspjstlparseexception

JSTL Number parsing error


I need the current year as an integer value (e.g. 2015) stored in a variable in a JSP page.
This is the JSTL tags i'm using:

<jsp:useBean id="date" class="java.util.Date" />
<fmt:formatDate var="currentYear" value="${date}" pattern="yyyy" />
<fmt:parseNumber var="currentYear" integerOnly="true" type="number" value="${currentYear}" parseLocale="en-US"/>

And this is the error I'm facing in the Tomcat Log:

[javax.servlet.ServletException: javax.servlet.jsp.JspException: In <parseNumber>, value attribute can not be parsed: "Wed Apr 29 00:42:30 CEST 2015"] with root cause
java.text.ParseException: Unparseable number: "Wed Apr 29 00:42:30 CEST 2015"
    at java.text.NumberFormat.parse(NumberFormat.java:350)
    at org.apache.taglibs.standard.tag.common.fmt.ParseNumberSupport.doEndTag(ParseNumberSupport.java:164)
    at org.apache.jsp.new_.reports_jsp._jspx_meth_fmt_005fparseNumber_005f0(reports_jsp.java:242)
    at org.apache.jsp.new_.reports_jsp._jspService(reports_jsp.java:136)

Am I doing something wrong?
The strangest thing is that I see this error only in the log: if I load the page, everything works as it should and I see the correct current year.


Solution

  • In the end I used an alternative workaround for my problem:

    <jsp:useBean id="date" class="java.util.Date" />
    <c:set var="currentYear" value="${date.getYear() + 1900}" />