简体   繁体   English

Taglib显示java.time.LocalDate格式

[英]Taglib to display java.time.LocalDate formatted

I would like to display formatted java.time.LocalDate in my JSP. 我想在我的JSP中显示格式化的java.time.LocalDate Do you know any taglib to use for this? 你知道用于此的任何taglib吗?

For java.util.Date we were using <%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %> . 对于java.util.Date我们使用<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %> Does something similar for java.time.LocalDate exist? 是否存在类似于java.time.LocalDate

Afsun's hints inspired me to create a quick solution. Afsun的提示激励我创建一个快速的解决方案。

  1. Under /WEB-INF create directory tags . /WEB-INF创建目录tags
  2. Create tag file localDate.tag inside the tags directory. tags目录中创建标记文件localDate.tag
  3. Put bellow code into this tag file: 将波纹管代码放入此标记文件中:

     <%@ tag body-content="empty" pageEncoding="UTF-8" trimDirectiveWhitespaces="true" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ attribute name="date" required="true" type="java.time.LocalDate" %> <%@ attribute name="pattern" required="false" type="java.lang.String" %> <c:if test="${empty pattern}"> <c:set var="pattern" value="MM/dd/yyyy"/> </c:if> <fmt:parseDate value="${date}" pattern="yyyy-MM-dd" var="parsedDate" type="date"/> <fmt:formatDate value="${parsedDate}" type="date" pattern="${pattern}"/> 
  4. Go to the JSP file in which you want display the java.time.LocalDate . 转到要在其中显示java.time.LocalDate的JSP文件。

    4.1. 4.1。 Add taglib directive <%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %> at the top of the file. 在文件顶部添加taglib指令<%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %>

    4.2. 4.2。 Use the localDate tag as follows: 使用localDate标记,如下所示:

    • <tags:localDate date="${yourDateToPrint}"/>
    • <tags:localDate date="${yourDateToPrint}" pattern="${yourPatternFormat}"/>

You can do this by fmt:parseDate .Try following: 您可以通过fmt:parseDate执行此操作。请fmt:parseDate以下操作:

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

<fmt:formatDate value="${parsedDate}" var="newParsedDate" type="date" pattern="dd.MM.yyyy" />

I hope this help you. 我希望这对你有帮助。 More information 更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM