• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unparseable Date

 
Andrew Babaian
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having trouble formating a date on a JSP that returns
data back from an Access database. I'm looking for a simple
MM/dd/yyyy or MM/dd/yy format. I get the date back in the
format, 2003-12-13 00:00:00.0 when I don't attmept to format it.
In the servlet that returns the date, I have:
-------------------------------------------------------------------
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
dateFormat.setLenient(false);
java.util.Date date = dateFormat.parse(rsLastTimeEntry.getString("Week ended"));
//The JSP stops returning at this line and no other
//data shows on the page, but the servlet still compiles
out.println("<TD>" + dateFormat.format(date) + "</TD>");
--------------------------------------------------------------
I get this error on the server....
java.text.ParseException: Unparseable date: "2003-12-13 00:00:00"

Any help would be appreciated.
Thanks
Andrew
 
Joe Ess
Bartender
Posts: 9295
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The date String you are passing in to the parse method isn't in the format "MM/dd/yyyy". You want to create one SimpleDateFormat instance with the format string "yyyy-MM-dd hh:mm:ss" to parse a String into a Date object and use the one you have above to format the Date object into a String.
[ December 12, 2003: Message edited by: Joe Ess ]
 
Andrew Babaian
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the help. it works now.
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
dateFormat.setLenient(false);
java.util.Date date = dateFormat.parse(rsLastTimeEntry.getString("Week ended"));
SimpleDateFormat shortDate = new SimpleDateFormat("MM/dd/yyyy");
shortDate.setLenient(false);
out.println("<TD>" + shortDate.format(date) + "</TD>");
-------------------
Andrew
 
Wayne L Johnson
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the "Week ended" column a varchar or date field in the database? If it's a date, you can just do:

This obviates the need to convert from a String to a Date and back to a different String. Let the database give you a date directly.
If the field is a varchar, then what you are currently doing is fine.
 
Andrew Babaian
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The data format is Date, and that simplifid way of writing it works. Thank you.
 
Andrew Babaian
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also notice you can use the Date class of the java.sql package instead. For some reason I could not find this in the documentation.
Thanks again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic