14

Is it possible to create a date value in JSTL Expression Language (EL) without using scriptlets? Here is a snippet of some of the legacy code I'm trying to refactor to only use EL.

<td><%=new java.text.SimpleDateFormat("MM/dd/yyyy HH:mm z").format(new java.util.Date())%></td>

Apparently it prints out the current date and time. I know I can format a date using EL, but can I get a date using EL?

1
  • @BalusC great reference, thanks for the link
    – bakoyaro
    Dec 28, 2010 at 14:52

2 Answers 2

23

I don't think you can do this in EL. But how about this, no scriptlets here

<jsp:useBean id="today" class="java.util.Date" scope="page" />
<fmt:formatDate value="${today}" pattern="MM.dd.yyyy" />
5
  • ah, this may be it. I'll test it out and let you know how it goes
    – bakoyaro
    Dec 22, 2010 at 19:12
  • please can you help me how to apply ${today} with pattern i want to do this but without substring <c:forEach var="year" items="${LK_YEAR}"> <c:if test="${year.year < fn:substring(currYear,24,29) }"> <option value="${year.year}">${year.year}</option> </c:if> </c:forEach>
    – shareef
    Jul 4, 2013 at 10:55
  • Is "Today" the date on the server side or the client side in this case? I would think Server
    – njfife
    Oct 23, 2013 at 21:35
  • @njfife today is on server side as all JSPs are executed on the server Oct 23, 2013 at 23:28
  • Yeah, I don't think it is even possible to get the client side date from a JSP, you would have to do it in the Java script... oh well
    – njfife
    Oct 24, 2013 at 16:28
1

Thid would be helpfull if you use spring webflow framework

if you define this on the flow.xml

<on-start>
    <set name="flowScope.now" value="new java.util.Date()" />
</on-start>

You can get the value like this

<fmt:formatDate value="#{now}" pattern="MM.dd.yyyy" />

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.