This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes String to date format Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "String to date format" Watch "String to date format" New topic
Author

String to date format

Krishnareddy Yeruva
Ranch Hand

Joined: Sep 17, 2008
Posts: 40
Hi ,
I have the date in string format like string date = "20101222"
Now I want to convert it to MM/dd/yyyy format.
I am getting "null" while i am trying to do it by using the following code




public static Date getDate(String date){
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
try{
return sdf.parse(date);
}catch(Exception e){
System.out.println("Error While parsing the date"+ e.getMessage());
}
}

So can you please help me out regarding this?




Thanks & Regards
KITTU
Mohamed Sanaulla
Bartender

Joined: Sep 08, 2007
Posts: 2927
    
  15

You need to have a String which can be parsed into the required format.

Also have a look at JavaDatesFaq


Mohamed Sanaulla | My Blog
S. Nitesh
Ranch Hand

Joined: Jan 15, 2010
Posts: 31

Your SimpleDateFormat is expecting the input date String in "yyyyMMdd" format and you are passing date
String of format "MM/dd/yyyy" .

Try this
Krishnareddy Yeruva
Ranch Hand

Joined: Sep 17, 2008
Posts: 40
Hi Nitesh and Mohamed,
Thanks for your reply..
What you said is right. But I want to display the given date(20101222) to the format like ("MM/dd/yyyy");
Is there anyway to do like this?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

Use another DateFormat to format the parsed Date back into a String.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Krishnareddy Yeruva
Ranch Hand

Joined: Sep 17, 2008
Posts: 40
Thanks Rob,
It is working. Thanks a lot
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String to date format
 
Similar Threads
comparing date
Date formatting
SimpleDateFormat
Date Confusion
Date formatting