• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to convert this String into a readable date?  RSS feed

 
Andrew Carney
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I got a String in hand in this format: yyyymmdd, for example: 19821117 or 19540227. I would like to turn it into a readable date in this format: dd.mm.yyyy. So 19821117 for example would be converted to: 17.11.1982.
I don't need the result to be a valid Date object, a String is also fine.
Any ideas...?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's what the java.text.SimpleDateFormat class does. Its parse method converts a String (in a given format) to a Date, while format converts a Date to a String.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A SimpleDateFormat works both to convert a String to a Date (via the parse() method) and a Date to a String (via the format() method).
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The parse and format technique is good OO stuff. Try it and see how it works. But simple string manipulation may be 100x as fast. Look at String.substring() to see how to get the parts out.

String mm = inputDate.substring ...
String dd = inputDate.substring ...
String yyyy = inputDate.substring ...
 
Andrew Carney
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,

Thanks for the tip, I am familiar with SimpleDateFormat.
The problem is that when I create one:
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy");
And than format my String I get: "Cannot format given Object as a Date"
So, can someone please show me a working code example?

Thanks,
Roy
[ August 08, 2007: Message edited by: Roy Cohen ]
 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to parse() to get the Date object and not format().
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should be something like:
 
Andrew Carney
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it!
Thanks for the quick response!
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you know that the format of the input string is always "yyyyMMdd" then you can also do this with some simple substring(...) calls - no need to parse it into a Date object and format it back.

As Stan already mentions above, this might work much faster than parsing it into a Date object and formatting it back into a String.

[ August 08, 2007: Message edited by: Jesper Young ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!