File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Simple Date conversion problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Simple Date conversion problem" Watch "Simple Date conversion problem" New topic

Simple Date conversion problem

Hardik Trivedi
Ranch Hand

Joined: Jan 30, 2010
Posts: 252
Hi all genius people,

I am stuck at very simple problem.I have a string in which time is stored in yyyy-MM-dd hh:mm:ss format.
What I need is to convert that string in Date class's object.So it is obvious that I shuild use SimpleDateFormat class

I create the object of that class and use it's parse method.I have written it in try catch block and it is showing Parsing error's exception.

What I have done is...

SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss aa");
java.util.Date dt=sdf.parse("2010-03-31 11:00:00");
System.out.println("Converted date is --> "+dt);

Can any one help me...

Thank you in advance.
T Eakins

Joined: Feb 20, 2004
Posts: 9
Try this code block, changing the format as required:

T Eakins

Joined: Feb 20, 2004
Posts: 9
I think you don't need the last 'aa'
Manohar Puttaswamy

Joined: Dec 25, 2009
Posts: 2

As you are using the am/pm marker you will have to specify that as well.
So the below date should work
dt = sdf.parse("2010-03-31 11:00:00 am");
Muhammad Khojaye
Ranch Hand

Joined: Apr 12, 2009
Posts: 449
It is sorta covered in the JavaRanch Style Guide.
subject: Simple Date conversion problem
It's not a secret anymore!