Search code examples
javaspringspring-mvcdatetime-formatspring-form

Date pattern with Spring MVC form tags


I have a entity class and it has java.util.Date type property. And when I pass "path" param to my input as defult it shows me like this :

enter image description here

But I want to change pattern of it. Also it must display existing Date value of selected owner object.

I tried to use fmt:formatDate but it doesnt work :/

<div class="form-group">
                        <label class="col-sm-3 control-label"><spring:message code="label.name" /><span class="text-danger">*</span></label>
                        <div class="col-sm-8">
                           <fmt:formatDate value="${owner.installDate}" var="installDate" pattern="dd/MM/yyyy" />
                           <form:input type="date" path="installDate"  value="${installDate}" class="form-control" placeholder="Yuklenme tarixini yazin....." required="true" />
                           <form:errors path="installDate" cssClass="error"></form:errors>
                        </div>


Solution

  • Just make sure you have following covered in your code.

    1. The tag library is added in your jsp

    2. Also make sure owner.installDate returns java.util.Date not String

    3. Add type="date"to your code

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

    4. Parse the date string first to java.util.Date as below

    <fmt:parseDate value="${owner.installDate}" pattern="yyyy-MM-dd HH:mm:ss" var="myDate"/>

    <fmt:formatDate value="${myDate}" var="installDate" type="date" pattern="dd/MM/yyyy" />