Formatting GregorianCalendar in EL with JSTL/fmt
Asked Answered
Q

1

6

I'm having a small problem with a JSP page. I'm using Stripes as framework but this should not be that relevant. Basically I have a bean that returns via a getter a date in the form of a GregorianCalendar. I have to display this date in JSP. When I try:

<fmt:formatDate type="both" dateStyle="full" value="${myObject.itsGregorian}">

I get an exception saying that he is unable to convert GregorianCalendar to Date.

I understand that fmt:formatDate formats a Date object and not a GregorianCalendar, but is there a way to turn around it? Since this is an assignment and I've got a pre-coded Bean I'm not allowed to touch the bean, so I can't transform its getter for the date to return a Date.

How can I solve this the best?

Quinn answered 8/4, 2011 at 21:3 Comment(0)
B
10

It indeed only supports java.util.Date. You need to call Calendar#getTime() to get it out the calendar.

<fmt:formatDate type="both" dateStyle="full" value="${myObject.itsGregorian.time}">
Bloodstained answered 8/4, 2011 at 21:4 Comment(1)
Just set a more specific style than full. Read the DateFormat javadoc for examples: download.oracle.com/javase/6/docs/api/java/text/DateFormat.html You can also use the pattern attribute. Read the SimpleDateFormat javadoc for examples: download.oracle.com/javase/6/docs/api/java/text/… If you stucks, just press Ask Question on right top :)Bloodstained

© 2022 - 2024 — McMap. All rights reserved.