We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

how to format a date to be displayed like mm/dd/yyyy

royalcyber
royalcyber asked
on
Medium Priority
366 Views
Last Modified: 2010-04-01
I have this text field where I am displaying value from a sql database

<td><input type="text" class="textfield" size="30" name= "EVENT_DATE" value=("<%= checkNull((String)selected_event.getEvent_date())%>"),       maxlength="80" readonly onClick="cal.select(document.forms['myform'].EVENT_DATE,'anchor','MM/dd/yyyy'); return false;">

The data is stored as date/time format in the database and shows something like this in the text field
1900-01-01 00:00:00.0

but I need it to displayed like this

01/01/1900

Any help will be greatly appreciated

Comment
Watch Question

Commented:
better is to format in sql query itself. Can you post your query and which db u r using?

Commented:
i m not available now..may be someone else can help.
Siva Prasanna KumarPrincipal Solutions Architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
check out the link.

http://javaalmanac.com/egs/java.text/FormatDate.html

i think its done as it was previous a accepted answer for me.
:-)
Siva Prasanna KumarPrincipal Solutions Architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
Format formatter;
formatter = new SimpleDateFormat("MM/dd/yy");
String s = formatter.format(date);

use the above java code in JSP before assinging it to text box do this formatting & then assign.
Principal Solutions Architect
CERTIFIED EXPERT
Top Expert 2006
Commented:
and one more thing is the below code expects you to give a date object

String s = formatter.format(date);

so while getting the date from data base result set use getDate() function of the result set and assign to date and then format the date and then assign to the text box.

Thank you.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Thank you very much

It works pretty good

Author

Commented:
I am sorry

when i restarted the browser i gives the following  error

org.apache.jasper.JasperException: Cannot format given Object as a Date


Would you know any way I can parse directly in JSP

Thanks

Siva Prasanna KumarPrincipal Solutions Architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
just put a try & catch block across the above code & check i think you may be getting a SQL Exception.
did you just use simple getDate() function or

 Date getDate(int columnIndex) ;
     
 Date getDate(int columnIndex, Calendar cal) ;

 Date getDate(String columnName) ;

 Date getDate(String columnName, Calendar cal) ;

which function have you tryed using???
     
and more on i don't think its a Date object which has been stored in your database it seems to be a timeStamp Object so try doing this and let me know if there is some problem

Date date = rs. getTimestamp(int columnIndex);

just replace the getDate() with the above statement.

Thank you
                       
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.