Format a date and display it using JSTL and EL

后端 未结 2 1654
栀梦
栀梦 2021-01-17 13:27

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.

相关标签:
2条回答
  • 2021-01-17 14:06

    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" %>

    0 讨论(0)
  • 2021-01-17 14:18

    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.

    0 讨论(0)
提交回复
热议问题