-1

In a jsp i would like to display the date of the year

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<jsp:useBean id="date" class="java.util.Date" pattern="yyyy" />
<fmt:formatDate value="${date}"  />

but i get

for servlet jsp threw exception
java.lang.IllegalArgumentException: Cannot convert 4/22/14 5:23 PM of type class     java.util.Date to Number

i saw on How to print current date in JSP? this is the way to go

so i don't really understand.

Community
  • 1
  • 1
redfox26
  • 2,020
  • 4
  • 25
  • 33

1 Answers1

3

Your issue

<jsp:useBean id="date" class="java.util.Date" pattern="yyyy" />

Should be

<jsp:useBean id="date" class="java.util.Date" />

And

<fmt:formatDate value="${date}"  />

Should be

<fmt:formatDate value="${date}" pattern="yyyy"/>

Why?

You're declaring a new Date object with the jsp:useBean tag. This does not require a pattern; Date is stored in it's own format. The pattern comes in when you want to output that Date object, using the fmt:formatDate tag. This requires a pattern so it knows how to output the date as a String, to the view.

christopher
  • 26,815
  • 5
  • 55
  • 89
  • It worked for me.. Remove other code except above three lines (suggested by @christopher) from your jsp and run it. – niiraj874u Apr 23 '14 at 06:09