*
The moose likes Java in General and the fly likes Example of  convert  String to Date Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Example of  convert  String to Date" Watch "Example of  convert  String to Date" New topic
Author

Example of convert String to Date

Kumar Naidu
Greenhorn

Joined: Jul 27, 2006
Posts: 11
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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.


[My Blog]
All roads lead to JavaRanch
 
jQuery in Action, 2nd edition
 
subject: Example of convert String to Date
 
Similar Threads
Problem in Coverting one Date Format to Other date Format
how to change the date,Time format after accessing it from the database
integer number too large:
error in formatting miliseconds for date objects
SimpleDateFormat