Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple Date conversion problem

 
Hardik Trivedi
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this code block, changing the format as required:



hth,
Tim
 
T Eakins
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you don't need the last 'aa'
 
Manohar Puttaswamy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 449
IntelliJ IDE Java Scala
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic