JSTL LocalDateTime format

I want to format a java 8 LocalDateTime object in the template "dd.MM.yyyy ". Is there any library for formatting? I tried the code below, but got a conversion exception.

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

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

+8
source share
5 answers

I actually had the same problem, and in the end I created the original Joda Time jsp tags to create Java 8 java.time JSP tags .

In this 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

+9
source

It does not exist in the 14 year old JSTL.

It is best to create a custom EL function. Create a useful method first.

 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 /WEB-INF/functions.tld in which 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 shown below:

 <%@taglib uri="http://example.com/functions" prefix="f" %> <p>Date is: ${f:formatLocalDateTime(date, 'dd.MM.yyyy')}</p> 

If necessary, extend the method for accepting the Locale argument.

+17
source

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 }" /> 
+4
source

Here is my solution (I am using Spring MVC).

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

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

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

 ${localDateTimeFormat.parse(date)} 

Now you can parse it using JSTL.

+4
source

I would suggest using java.time.format.DateTimeFormatter . First import it into JSP <%@page import="java.time.format.DateTimeFormatter" %> , then format the variable ${localDateTime.format( DateTimeFormatter.ofPattern("dd.MM.yyyy"))} . As a newbie to Java development, I am interested in making this approach acceptable from a β€œbest practice” perspective.

0
source

Source: https://habr.com/ru/post/1243763/


All Articles