Format a date and display it using JSTL and EL
Asked Answered
A

2

11

How do I format and display a Date object in a JSP, most preferably using JSTL and EL but any other solution is welcome? I can not change the bean object.

I have the following class:

import java.util.Date;
public class Person {
    private Date myDate; 
    public Date getMyDate() {
        return myDate;
    }
    public void setMyDate(Date myDate){
        this.myDate = myDate;
    }
}

I am trying to display the date in this object in a JSP page. When I do this <c:out value="${person.myDate} /> it prints this in the page. 2013-06-08 00:00:00.0

What I want to do is remove the time portion of the date and format it to MM-dd-yyyy.

I tried this:

<c:set var="myDate" value="${person.myDate }"/>
<fmt:formatDate value="${myDate}" type="date" var="formattedDate"/>

and it gave me the following error

Unable to convert string '${myDate}' to class java.util.Date for attribute value: java.lang.IllegalArgumentException: Property Editor not registered with the PropertyEditorManager

Then I tried the following:

<c:set var="myDate" value="${person.myDate }"/>
<fmt:parseDate value="${myDate }" var="parsedDate" pattern="MM-dd-yyyy"/>
<c:out value="${parsedDate }"/>

and I got:

Unparseable date: "${myDate }"

Asquith answered 29/5, 2013 at 20:15 Comment(0)
B
22

Your code should normally work. Try like this:

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

<fmt:formatDate value="${person.myDate}" var="formattedDate" 
                type="date" pattern="MM-dd-yyyy" />
${formattedDate}

It usually doesn't work if you have wrong JSTL declarations to match your Servlet/JSP version. Make sure you read this before trying anything else: How to Reference and Use JSTL in your Web Application.

If you don't know exactly your environment, you can perform some tests to find out the versions although a simple ${1 + 2} written in your JSP should be a good indicator of the JSP version. If you see 3 in your browser then you are using JSP 2.x, if you see the string ${1 + 2} instead then you are on JSP 1.x.

Birchard answered 29/5, 2013 at 20:37 Comment(3)
Gives me "Unable to convert string '${myDate}' to class java.util.Date"Asquith
As for wrong JSTL declarations, my page does display the date if I don't try to format it. Is that enough to to confirm that the jstl declarations match my servlet/jsp version?Asquith
No, tags changed in between JSP versions. They used to get a string (the EL) and evaluate it for themselves. Then in higher JSP versions the servlet container does the evaluation and then sends the object to the tag which receives that type of object and not a string to evaluate. If you mix the tags you will get weird errors. If one tag works it does not mean that the rest are fine.Birchard
W
0

You need to add the following line of code to the head of your page and your code will work perfect. <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

Wendolynwendt answered 28/12, 2014 at 11:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.