Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting date with fmt:formatDate

Tags:

java

jstl

I trying to format a date using fmt:formatDate.

 <c:forEach items="${list}" var="item">
     <tr>
       <!--some other columns-->
       <td align="left">
          <fmt:parseDate value="${item.date}" type="both" var="date"/>
          <fmt:formatDate value="${date}" type="both" pattern="dd/MM/yyyy H:m"/>
       </td> 
       <!--some other columns-->
      </tr>
  </c:forEach>

but I get this exception:

java.text.ParseException: Unparseable date: "2010-12-12 16:00:00"

All I want is to show the var date this way: 12/12/2010 16:00. How can I achieve this?

Thanks in advance.

like image 422
eddy Avatar asked Dec 28 '22 04:12

eddy


1 Answers

You need to specify the parse pattern.

<fmt:parseDate value="${item.date}" pattern="yyyy-MM-dd HH:mm:ss" var="date"/>
<fmt:formatDate value="${date}" pattern="dd/MM/yyyy HH:mm" />

Normal practice, however, is to store dates as java.util.Date, not as java.lang.String. Then you can just do:

<fmt:formatDate value="${item.date}" pattern="dd/MM/yyyy HH:mm" />

Use as much as possible the right type for the value. It will become more easy to handle it.

like image 115
BalusC Avatar answered Jan 05 '23 16:01

BalusC