• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

String to date format

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?



 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to have a String which can be parsed into the required format.

Also have a look at JavaDatesFaq
 
Ranch Hand
Posts: 36
1
VI Editor Chrome Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your SimpleDateFormat is expecting the input date String in "yyyyMMdd" format and you are passing date
String of format "MM/dd/yyyy" .

Try this
 
Krish Yeruva
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 21775
103
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use another DateFormat to format the parsed Date back into a String.
 
Krish Yeruva
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob,
It is working. Thanks a lot
 
It wasn't my idea to go to some crazy nightclub in the middle of nowhere. I just wanted to stay home and cuddle with this tiny ad:
Java Code Review and Psychology
https://coderanch.com/t/714798/java/Java-Code-Review-Psychology
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!