Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use format date as "yyyy-MM-dd" with JSTL? [duplicate]

Tags:

java

date

jsp

jstl

I want to take date from DB and display on jsp:

2014-04-02

instead of:

2014-04-02 00:00:00.0

On jsp I tried to use c:fmt tag for formatting date:

   <div class="form-group">
      <span><fmt:message key="task.start"/></span>
      <input class="form-control" id="firstDate" placeholder="<fmt:message key="task.start"/>" 
           name="start_date-${task.taskId}"
         <fmt:formatDate value="${task.startDate}" var="startFormat" type="date" pattern="yyyy-MM-dd"/>
        value="${startFormat}"/>
   </div>

Looking on the page:

view

How to format it to yyyy-MM-dd format?

like image 324
catch23 Avatar asked Apr 02 '14 22:04

catch23


1 Answers

First you need to add the line below to head of your jsp file

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

Now you can use <fmt:formatDate> and <fmt:parseDate> for formatting date.

<fmt:formatDate value="${now}" pattern="yy-MMM-dd"/>

PS: In your code, I saw you had some mistakes with the jsp tag. I think it should be

    <div class="form-group">
      <span><fmt:message key="task.start"/></span>
      <input class="form-control" id="firstDate" placeholder="<fmt:message key='task.start'/>" 
           name="start_date-${task.taskId}" value="<fmt:formatDate value='${task.startDate}' var='startFormat' type='date' pattern='yyyy-MM-dd'/>"
   </div>
like image 112
tiny4penguin Avatar answered Sep 29 '22 04:09

tiny4penguin