Taglib to display java.time.LocalDate formatted

I would like to display formatted java.time.LocalDate in my JSP. Do you know any taglib for this?

In java.util.Date we used <%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %> . Does something like this exist for java.time.LocalDate ?

+8
java date java-8 jsp taglib
source share
3 answers

Afsun's tips inspired me to create a quick fix.

  • In /WEB-INF create the tags directory.
  • Create a localDate.tag tag localDate.tag inside the tags directory.
  • Paste this code into this tag file:

     <%@ tag body-content="empty" pageEncoding="UTF-8" trimDirectiveWhitespaces="true" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ attribute name="date" required="true" type="java.time.LocalDate" %> <%@ attribute name="pattern" required="false" type="java.lang.String" %> <c:if test="${empty pattern}"> <c:set var="pattern" value="MM/dd/yyyy"/> </c:if> <fmt:parseDate value="${date}" pattern="yyyy-MM-dd" var="parsedDate" type="date"/> <fmt:formatDate value="${parsedDate}" type="date" pattern="${pattern}"/> 
  • Go to the JSP file in which you want to display java.time.LocalDate .

    4.1. Add the taglib directive <%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %> to the top of the file.

    4.2. Use the localDate tag as follows:

    • <tags:localDate date="${yourDateToPrint}"/>
    • <tags:localDate date="${yourDateToPrint}" pattern="${yourPatternFormat}"/>
+10
source share

You can do this with fmt:parseDate . Try the following:

 <fmt:parseDate value="${dateForParsing}" pattern="yyyy-MM-dd" var="parsedDate" type="date" /> <fmt:formatDate value="${parsedDate}" var="newParsedDate" type="date" pattern="dd.MM.yyyy" /> 

Hope this helps you. Additional Information

+3
source share

One solution would be to use the @XmlJavaTypeAdapter (LocalDateAdapter.class) annotation in your javabean:

 @XmlJavaTypeAdapter(LocalDateAdapter.class) public LocalDate getLoanDate() { return loanDate; } 
0
source share

All Articles