Click here to Skip to main content
15,938,446 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to convert a String like "05/30/2012" to a Date as same like "05/30/2012" and im using these codes.

Java
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
Date d1=formatter.parse("05/30/2012");
System.out.println(d1);


But these codes compile result is :
"Wed May 30 00:00:00 EEST 2012"
I need "05/30/2012".Thanks for any help.

Note: Guys iam trying to convert String to Date.I have a Date column in Database.I dont want to change it's format.I just want 05/30/2012 in my database but as a Date class not a String
Posted
Updated 14-May-12 1:29am
v2

Please refer following CP Article:
Formatting a DateTime for display - format string description[^]

also refer DateTime.Parse Method (String)[^]

or else
You can parse user input like this:
C#
DateTime enteredDate = DateTime.Parse(enteredString);

If you have a specific format for the string, you should use the other method:
C#
DateTime loadedDate = DateTime.ParseExact(loadedString, "d", null);

"d" stands for the short date pattern (see MSDN[^] for more info) and null specifies that the current culture should be used for parsing the string.

[EDIT]
For Java:
Please refer following threads:
Convert String to Date[^]
JavaScript Date Format[^]
JavaScript Data Object Examples: Javascript Convert String to Date[^]

Have a look on this Similar discussion[^]

Hope it helps!
 
Share this answer
 
v2
Comments
SercanOzdemir 14-May-12 7:30am    
i need it in Java and please read again i edited question.
Prasad_Kulkarni 14-May-12 7:43am    
please see updated answer..
Ed Nutting 14-May-12 7:45am    
The problem lies in the OP's use of println not in the date parsing - it is parsed correctly but printed in the console in a format he doesn't like. You need to provide links to Date to String conversion for OP to get the answer to his real problem. My 4+ anyway...
Ed
Prasad_Kulkarni 14-May-12 7:49am    
Thanks Ed Nutting!
hi,
try like this:

C#
DateTime date = DateTime.Now;
        String.Format("{0:MM/dd/yyyy}", date); 
 
Share this answer
 
Comments
SercanOzdemir 14-May-12 7:30am    
Please read question again i edited it.
tanweer 14-May-12 7:49am    
sorry we are away from JAVA!!!!!!!!
u can also try :
DateTime.Now.ToString("MM/dd/yyyy");


i.e convert required string in datetime object and then use .ToString("MM/dd/yyyy").
 
Share this answer
 
Comments
SercanOzdemir 14-May-12 7:31am    
No i need it in JAVA!please read question and examine Tags.
I've solved!!
I changed it to java.sql.Date format and now it shows 05/30/2012 Thanks anyway.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900