wood burning stoves 2.0*
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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Parsing date using SimpleDateFormat.Please help" Watch "Parsing date using SimpleDateFormat.Please help" New topic
Author

Parsing date using SimpleDateFormat.Please help

Vidya Pen
Greenhorn

Joined: Jul 24, 2006
Posts: 3
Hi,

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
Vidya
mini meenakshi
Greenhorn

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();
System.out.println(df.format(d1));
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: 14074
    
  16

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 7 API documentation
Scala Notes - My blog about Scala
 
 
subject: Parsing date using SimpleDateFormat.Please help
 
Similar Threads
Parsing date using SimpleDateFormat for hh mm ss
date format conversion to MM/dd/yyyy hh:mm a
Date Conversion
Date Formatting
comparing dates