0

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

Ashok Charu
  • 274
  • 2
  • 23
  • @JasperdeVries Already I tried with your suggested Question but is not Working – Ashok Charu May 30 '18 at 11:33
  • Tried what? Nothing about that in your question. Please [edit] it and update it with what you tried. Just curious: where does `${COLL.date}` come from? It looks like it is already formatted from a date object. – Jasper de Vries May 30 '18 at 11:41
  • @JasperdeVries ${COLL.date} this is the variable, I'm getting from DB. I already posted, What I tried – Ashok Charu May 30 '18 at 11:51
  • So, in your database, you are not storing it as a date type? It would be better to have a date type. Then, your `parseDate pattern` should be `yyyyMMdd` without the slashes. – Jasper de Vries May 30 '18 at 12:02
  • @JasperdeVries I'm using MongoDB and I'm not doing for insertion part. – Ashok Charu May 30 '18 at 12:04

1 Answers1

1

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" />
Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
  • I tried With Date, not time, I got output like this Tue Jul 19 00:00:00 IST 2016. But I'm looking for this format 2016/07/19 and also I want to date & time separate variable – Ashok Charu May 30 '18 at 12:23
  • Using a variable is optional but certainly not required. You could use them if you like, but it will not effect formatting in any way. – Jasper de Vries May 30 '18 at 14:58