JSP fmt:formatDate increase value of Date

3.1k Views Asked by At

I was creating a .tagx file such that it would render a selection of days (within 30-days period) without passing the collection of java.util.Date.

So far here's what I've done. I can't get the date variable to increase the value of day by one. I've tried to re-set the value of date using <c:set> tag with the formula index * (24 * 24 * 60 * 1000) and it returns javax.el.ELException on that line.

Here's the code I've done so far.

<code>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page"
xmlns:c="http://java.sun.com/jsp/jstl/core" 
xmlns:fn="http://java.sun.com/jsp/jstl/functions"
xmlns:fmt="http://java.sun.com/jstl/fmt_rt" 
xmlns:spring="http://www.springframework.org/tags" 
xmlns:form="http://www.springframework.org/tags/form" 
version="2.0">
<jsp:output omit-xml-declaration="yes" />
<jsp:useBean id="date" class="java.util.Date" />

<jsp:directive.attribute name="path" type="String" required="true" rtexprvalue="true" description="The name and id of select tag" />
<jsp:directive.attribute name="classVal" type="String" required="true" rtexprvalue="true" description="The class style to be used by this select" />
<jsp:directive.attribute name="noOfDays" type="Integer" required="true" rtexprvalue="true" description="Number of days to be printed on the selection" />

<select id="${path }" name="${path }" class="${classVal }">
    <option value=""></option>
    <c:forEach var="index" begin="1" end="${noOfDays }">
        <fmt:formatDate value="${date }" pattern="yyyy/MM/dd" var="dateKey" />
        <fmt:formatDate value="${date }" pattern="MM/dd (EEE)" var="dateValue" />
        <option value="${dateKey }">${dateValue }</option>
    </c:forEach>
</select>

</jsp:root>
</code>
2

There are 2 best solutions below

0
On

I've figured out a way that can solve this by inserting this code at the end of the loop

<jsp:setProperty property="time" name="date" value="${date.getTime() + (24*60*60*1000)}" />
0
On

I ran into the same problem when I used fmt:formatDate with Expression Language (EL) like this

<fmt:formatDate pattern="yyyy" value="${coverDate}" var="year" />

and the error was something tike this

 EL expression '${coverDate}' is only allowed for attributes with rtexprvalue='true'.

the whole problem was just because of a wrong import

<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>

and the correct import should be

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

hope this saves your time