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?

informatik01
  • 16,038
  • 10
  • 74
  • 104
Ugur Artun
  • 1,744
  • 2
  • 23
  • 41

5 Answers5

27

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.

Bastien Jansen
  • 8,756
  • 2
  • 35
  • 53
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
15

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

sargue
  • 5,695
  • 3
  • 28
  • 43
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 }" />
Astik Anand
  • 12,757
  • 9
  • 41
  • 51
egemen
  • 779
  • 12
  • 23
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.

aleric
  • 152
  • 4
  • 9
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'.