Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Parsing date using SimpleDateFormat.Please help

 
Vidya Pen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15492
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic