JSTL LocalDateTime format
Asked Answered
R

5

16

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?

Robbynrobe answered 24/2, 2016 at 15:44 Comment(1)
See also: github.com/sargue/java-time-jsptagsSuperintendency
B
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

Bui answered 8/4, 2016 at 11:13 Comment(1)
I have tried, this is successful and it has easy shortcut for parsing and formating.Gelasius
R
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.

Reputed answered 24/2, 2016 at 16:12 Comment(0)
B
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

Bui answered 8/4, 2016 at 11:13 Comment(1)
I have tried, this is successful and it has easy shortcut for parsing and formating.Gelasius
G
11

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 }" />
Gelasius answered 27/5, 2016 at 6:51 Comment(1)
nice this works great with jstl sql datetimeMissionary
S
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.

Slogan answered 9/6, 2017 at 8:47 Comment(0)
S
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'.

Sothena answered 17/6, 2019 at 16:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.