17

I want to format my Java 8 LocalDateTime object in "dd.MM.yyyy" pattern. Is there any library to format? I tried code below but got conversion exception.

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

Is there any tag or converter for LocalDateTime class in JSTL?

1

5 Answers 5

28

It doesn't exist in the 14-year old JSTL.

Your best bet is creating a custom EL function. First create an utility method.

package com.example;

public final class Dates {
     private Dates() {}

     public static String formatLocalDateTime(LocalDateTime localDateTime, String pattern) {
         return localDateTime.format(DateTimeFormatter.ofPattern(pattern));
     }
}

Then create a /WEB-INF/functions.tld wherein you register the utility method as an EL function:

<?xml version="1.0" encoding="UTF-8" ?>
<taglib 
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
    version="2.1">

    <tlib-version>1.0</tlib-version>
    <short-name>Custom_Functions</short-name>
    <uri>http://example.com/functions</uri>

    <function>
        <name>formatLocalDateTime</name>
        <function-class>com.example.Dates</function-class>
        <function-signature>java.lang.String formatLocalDateTime(java.time.LocalDateTime, java.lang.String)</function-signature>
    </function>
</taglib>

Finally use it as below:

<%@taglib uri="http://example.com/functions" prefix="f" %>

<p>Date is: ${f:formatLocalDateTime(date, 'dd.MM.yyyy')}</p>

Extend if necessary the method to take a Locale argument.

17

Actually I had the same problem and ended up forking the original Joda Time jsp tags to create Java 8 java.time JSP tags.

With that library your example would be something like this:

<javatime:parseLocalDateTime value="${date}" pattern="yyyy-MM-dd" var="parsedDate" />

Check the repository for installation instructions: https://github.com/sargue/java-time-jsptags

1
  • I have tried, this is successful and it has easy shortcut for parsing and formating.
    – egemen
    Nov 26, 2018 at 8:57
10

No, It does not exist for LocalDateTime.

However, you can use:

<fmt:parseDate value="${ cleanedDateTime }" pattern="yyyy-MM-dd'T'HH:mm" var="parsedDateTime" type="both" />
<fmt:formatDate pattern="dd.MM.yyyy HH:mm" value="${ parsedDateTime }" />
1
5

Here is my solution (I'm using Spring MVC).

In the controller add a SimpleDateFormat with the LocalDateTime pattern as a model attribute:

model.addAttribute("localDateTimeFormat", new SimpleDateFormat("yyyy-MM-dd'T'hh:mm"));

Then use it in the JSP to parse the LocalDateTime and get a java.util.Date:

${localDateTimeFormat.parse(date)}

Now you can parse it with JSTL.

3

I'd suggest using java.time.format.DateTimeFormatter. First import it to JSP <%@ page import="java.time.format.DateTimeFormatter" %>, then format variable ${localDateTime.format( DateTimeFormatter.ofPattern("dd.MM.yyyy"))}. Being new to java development, I'm interested wether this approach is acceptable in terms of 'best practice'.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.