【问题标题】:JSTL LocalDateTime formatJSTL LocalDateTime 格式
【发布时间】:2016-02-24 15:44:48
【问题描述】:

我想以 "dd.MM.yyyy" 模式格式化我的 Java 8 LocalDateTime 对象。有没有要格式化的库?我尝试了下面的代码,但出现了转换异常。

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

JSTL 中有LocalDateTime 类的标签或转换器吗?

【问题讨论】:

标签: jsp jstl date-format java-time


【解决方案1】:

14岁的JSTL中不存在。

最好的办法是创建一个自定义 EL 函数。首先创建一个实用方法。

package com.example;

public final class Dates {
     private Dates() {}

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

然后创建一个/WEB-INF/functions.tld,在其中将实用程序方法注册为 EL 函数:

<?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>

最后使用如下:

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

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

如有必要,扩展方法以采用 Locale 参数。

【讨论】:

    【解决方案2】:

    其实我也遇到了同样的问题,最后把原来的 Joda Time jsp 标签分叉来创建Java 8 java.time JSP tags

    使用该库,您的示例将是这样的:

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

    查看存储库以获取安装说明:https://github.com/sargue/java-time-jsptags

    【讨论】:

    • 我试过了,成功了,解析格式化的快捷方式很简单。
    【解决方案3】:

    不,LocalDateTime 不存在。

    但是,您可以使用:

    <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】:

      这是我的解决方案(我使用的是 Spring MVC)。

      在控制器中添加一个带有 LocalDateTime 模式的 SimpleDateFormat 作为模型属性:

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

      然后在JSP中使用它来解析LocalDateTime,得到一个java.util.Date:

      ${localDateTimeFormat.parse(date)}
      

      现在你可以用 JSTL 解析它了。

      【讨论】:

        【解决方案5】:

        我建议使用java.time.format.DateTimeFormatter。 首先将其导入到JSP &lt;%@ page import="java.time.format.DateTimeFormatter" %&gt;,然后格式化变量${localDateTime.format( DateTimeFormatter.ofPattern("dd.MM.yyyy"))}。 作为 Java 开发的新手,我很感兴趣这种方法在“最佳实践”方面是否可以接受。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2022-10-13
          • 1970-01-01
          • 1970-01-01
          • 2019-05-19
          • 1970-01-01
          • 2020-12-20
          • 2021-12-19
          相关资源
          最近更新 更多