Search code examples
jstl

Add slash in DATE YYYMMDD and Colon in TIME HHmm in jstl


I'm getting two variables From DB Date and time (${COLL.date},${COLL.time}).

The two variables values are like this 20160719 and 1234

I want to format this two variables like this 2016/07/19 and 12:34

In my JSP page, I haded this library

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

And I set tag

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

<p>${dateformat}</p>

This is my variable ${COLL.date} (20160719), Which I'm getting from my DB.

When I do like above, I'm getting an ERROR


Solution

  • OK, so you've followed the instructions from the answer on Convert and format a Date in JSP. Your parseDate format is wrong though. And, you could throw in the time as well in one go, so:

    <fmt:parseDate pattern="yyyyMMdd HHmm" value="${COLL.date} ${COLL.time}" var="parsedDate" />
    <fmt:formatDate value="${parsedDate}" pattern="yyyy/MM/dd HH:mm" />
    

    If you need to output the date and time separately, use:

    <fmt:parseDate pattern="yyyyMMdd HHmm" value="${COLL.date} ${COLL.time}" var="parsedDate" />
    <fmt:formatDate value="${parsedDate}" pattern="yyyy/MM/dd" />
    <fmt:formatDate value="${parsedDate}" pattern="HH:mm" />