I have a tag with the following:
<%@ tag body-content="empty"%>
<%@ attribute name="timestamp" required="true" type="java.sql.Timestamp"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<jsp:useBean id="dateValue" class="java.util.Date" />
<c:if test="${not empty timestamp}">
<jsp:setProperty name="dateValue" property="time" value="${timestamp}" />
<span title="${timestamp}"> <fmt:formatDate value="${dateValue}"
pattern="MM/dd/yyyy HH:mm" /> </span>
</c:if>
I get the following error however:
Error 500: com.ibm.ws.jsp.JspCoreException: java.lang.IllegalArgumentException: Cannot convert 5/1/12 10:36 AM of type class java.sql.Timestamp to long
I was trying to follow this answer to convert a timestamp to a date in JSTL, so I wouldn’t have change anything in my servlet. How can I convert a java.sql.Timestamp to a date so that formatDate can work with it, using JSTL?
You need to pass in
Timestamp#getTime().But this makes all no sense. The
java.sql.Timestampis already a subclass ofjava.util.Date. So this should also do:I’d by the way also change your models to declare the property as
java.util.Dateinstead. You should not usejava.sql.Timestampin the model and view, but only in the data layer. You don’t need to convertResultSet#getTimestamp()tojava.util.Dateby parsing/formatting. Just upcasting is sufficient.E.g.
with