0

I have a transfer object being returned to the JSP after a search. It is having a java.util.Date field (e.g. private Date issueDate;)

I am accessing the data in TO using usebean tag and displaying the date as:

<INPUT TYPE="text" readonly="readonly" NAME="issueDt" ID="issueDt" 
       SIZE="45" value="<%=mySearchTO.getIssueDt()%>">

However, this is printing the date in the format say for e.g. MON JAN 31 00:08:00 IST 2011

I want the date to be printed simply as MM/DD/YYYY and in the cases where time is also important, in the MM/DD/YYYY HH:MM format.

How to achieve this inside JSP? I don't know if I need to go for Javascript function or some static Java method.

Please excuse the usage of scriptlet. It's a legacy application and so I can not use EL. Please provide solution through scriptlet only. So solutions like:

<fmt:formatDate value="${new Date(c.dateInIntegerValue)}" 
                pattern="dd.MM.yyyy hh:mm"/> 

available in other questions, will not work for me.

Is the following code valid?

<fmt:formatDate value="<%=mySearchTO.getIssueDt()%>" 
                pattern="dd.MM.yyyy hh:mm"/> 

If yes, how to use it in the JSP? I mean label and all!

Also as far as possible, I want to avoid usage of jquery and such libraries.

Piotr Nowicki
  • 17,914
  • 8
  • 63
  • 82
Vicky
  • 16,679
  • 54
  • 139
  • 232
  • Why can't you use EL and JSTL? What's your version of jsp-api? Name your servlet container and version as well. – Sahil Muthoo Nov 19 '11 at 11:15
  • There are 100's of legacy JSP pages with scriptlets in the application. So I don't want to introduce EL in just one page where I want change and disturb the "sanity" of the legacy app :) – Vicky Nov 19 '11 at 11:16
  • Are you using custom tag libraries? Maybe you could put your code over there? Does your project have any files ending in `.tld`? – Sahil Muthoo Nov 19 '11 at 11:26
  • no.. no files ending in .tld... – Vicky Nov 19 '11 at 11:40
  • It's **not different** from how you do it in a normal Java class. Asking this question in JSP/Scriptlet context is basically irrelevant. Just ask it in the future in Java context. – BalusC Nov 19 '11 at 17:14

4 Answers4

5
<%@ page import="java.text.SimpleDateFormat" %>    
<% SimpleDateFormat dateFormatWithTime = new SimpleDateFormat("MM/dd/yyyy hh:mm");%>
<INPUT TYPE="text" readonly="readonly" NAME="issueDt" ID="issueDt" SIZE="45" value="<%=dateFormatWithTime.format(mySearchTO.getIssueDt())%>">

Ideally you should just use formatDate from JSTL or factor out this code into a custom taglib.

Sahil Muthoo
  • 12,033
  • 2
  • 29
  • 38
0

You will need to use java.text.SimpleDateFormat class or Joda time.

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
0

Use DateFormat

http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html

Date date = new Date();
SimpleDateFormat df = new SimpleDateFormat();
df.applyPattern("MM/dd/yyyy");
System.out.println(df.format(date));
Codezilla
  • 400
  • 1
  • 7
Tao Song
  • 133
  • 7
0

Use ..

DateFormat f = new SimpleDateFormat("yyyy-MM-dd"); .

Tushar Agarwal
  • 521
  • 1
  • 16
  • 39