Manesh
Manesh

Reputation: 596

Setting value of input type date in jsp

I'm trying to set the value in my input field of type date in my jsp page. The value is fetched from database is of type String(varchar) I want to set this date value to my input field.

My Jsp script is given below.

<%
      Op_Product opp = new Op_Product();
      String pubdate = null;
      ResultSet rse = opp.getOneProductWithId(request.getParameter("prdid"));
      while (rse.next()) {                            
             pubdate = rse.getString("prdf7"); // value to set to input field                            
      }
%>

My html input type date field is given below.

<input id="d-pubdate" type="date" class="form-control" name="pubDate" value="<%=pubdate%>"required>

The error I'm getting on this is "Bad value for attribute, The literal did not satisfy the date format".

The format is "YYYY-MM-DD".

Upvotes: 2

Views: 10877

Answers (1)

Braj
Braj

Reputation: 46841

In JSP, you can use JSTL fmt tag library that provides a set of tags for parsing and formatting locale-sensitive numbers and dates.

Read more Oracle Tutorial - Internationalization Tag Library and JSP Standard Tag Library

Sample code:

<c:set value="10/23/2014 - 15:15:22" var="dateString" />

<fmt:parseDate value="${dateString}" var="dateObject"
                                      pattern="MM/dd/yyyy - HH:mm:ss" />

<fmt:formatDate value="${dateObject}" pattern="dd/MM/yyyy - hh:mm a" />

Upvotes: 2

Related Questions