This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring Boot in Practice and have Somnath Musib on-line!
See this thread for details.
Win a copy of Spring Boot in Practice this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Parsing date using SimpleDateFormat.Please help

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic