Meaningless Drivel is fun!*
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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Simple Date conversion problem" Watch "Simple Date conversion problem" New topic
Author

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
Greenhorn

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



hth,
Tim
T Eakins
Greenhorn

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

Joined: Dec 25, 2009
Posts: 2
Hi,

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

http://faq.javaranch.com/java/JavaDatesFaq#parsing


http://muhammadkhojaye.blogspot.com/
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Simple Date conversion problem
 
Similar Threads
String to date conversion in java
Convert to timestamp yyyy-MM-dd hh:mm:ss a
Date Formatting
Timezone Conversion
Can't parse dates with full zone name