JSTL LocalDateTime format

☆樱花仙子☆ 提交于 2019-12-09 05:23:03

问题


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:


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




回答2:


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.




回答3:


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:


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.




回答5:


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'.



来源:https://stackoverflow.com/questions/35606551/jstl-localdatetime-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!