File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Unparseable Date Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Unparseable Date" Watch "Unparseable Date" New topic
Author

Unparseable Date

Andrew Babaian
Greenhorn

Joined: May 16, 2002
Posts: 25
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

Joined: Oct 29, 2001
Posts: 8710
    
    6

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 ]

"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Andrew Babaian
Greenhorn

Joined: May 16, 2002
Posts: 25
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

Joined: Sep 03, 2003
Posts: 399
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

Joined: May 16, 2002
Posts: 25

The data format is Date, and that simplifid way of writing it works. Thank you.
Andrew Babaian
Greenhorn

Joined: May 16, 2002
Posts: 25
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unparseable Date
 
Similar Threads
Date Problem when Formatting
<fmt:formatDate question.
issue with datetimepicker & autocompleter in struts 2
Convert to timestamp yyyy-MM-dd hh:mm:ss a
How to fetch Date Field from excel sheet in Java