• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Example of convert String to Date

 
Kumar Naidu
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all

i have some Problem covert String to Date i am sending My code also.

Date currentDatetime = new Date(System.currentTimeMillis());
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss aaa");
String myDate = formatter1.format(currentDatetime);
System.out.println(myDate);
Date ss =formatter1.parse(myDate);
System.out.println(ss);

O/p: 2006-09-04 11:41:07 AM
Mon Sep 04 11:41:07 GMT+05:30 2006 (I want same Format in Date Type also yyyy-MM-dd HH:mm:ss aaa");

Thanks&Regards
Vinay
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've got to use a formatter as in your first output, to get the desired format. Calling print on a Date instance will call Date.toString(), which uses "dow mon dd hh:mm:ss zzz yyyy" as the output format.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic