File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Parsing date using SimpleDateFormat.Please help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Parsing date using SimpleDateFormat.Please help" Watch "Parsing date using SimpleDateFormat.Please help" New topic

Parsing date using SimpleDateFormat.Please help

Vidya Pen

Joined: Jul 24, 2006
Posts: 3

I tried to parse the date using SimpleDateFormat

DateFormat df = new SimpleDateFormat("d MMM yyyy hh:mm");

i get the date from database using resultset.getstring()method
and the date (String) is

String strEndDate = "2006-08-14 03:57:00.0"

now i parse it as

Date expiryDate = df.parse(strEndDate);

but i get the exception : java.text.ParseException: Unparseable date: "2006-08-14 03:57:00.0"...

i need the date in the format "d MMM yyyy hh:mm"(the date and time format)
Please help!

Thanks in advance
mini meenakshi

Joined: Apr 10, 2006
Posts: 6
Hey ,
Following code will work
DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.S");

Date d1 = new Date();
String strEndDate = "2006-08-14 03:57:00.0";

Date expiryDate = df.parse(strEndDate);

You need to use above mentioned date format
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15101

The date string that you are trying to parse, "2006-08-14 03:57:00.0", obviously does not have the format "d MMM yyyy hh:mm", so it is to be expected that you get a parse exception.

Is the column in the database a DATE, DATETIME, TIMESTAMP, or similar type of column in the database? If yes, then why are you using ResultSet.getString(...) to get it from the database? It would be better to use getDate(), getTime() or getTimestamp() instead, these methods directly return a Date object so that you don't have to parse it at all.

If you need the date in the format "d MMM yyyy hh:mm", then you just format (not parse) the Date object using SimpleDateFormat.format(...).
[ August 07, 2006: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
I agree. Here's the link:
subject: Parsing date using SimpleDateFormat.Please help
It's not a secret anymore!