aspose file tools
The moose likes Java in General and the fly likes How to convert this String into a readable date? 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 "How to convert this String into a readable date?" Watch "How to convert this String into a readable date?" New topic
Author

How to convert this String into a readable date?

Andrew Carney
Ranch Hand

Joined: Oct 17, 2006
Posts: 96
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
Marshal

Joined: Mar 22, 2005
Posts: 35237
    
    7
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.


Android appsImageJ pluginsJava web charts
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16483
    
    2

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

Joined: Jan 29, 2003
Posts: 8791
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 ...


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Andrew Carney
Ranch Hand

Joined: Oct 17, 2006
Posts: 96
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

Joined: Sep 11, 2006
Posts: 959

You need to parse() to get the Date object and not format().


SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
My Blog
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35237
    
    7
Should be something like:
Andrew Carney
Ranch Hand

Joined: Oct 17, 2006
Posts: 96
Got it!
Thanks for the quick response!
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12911
    
    3

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 ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: How to convert this String into a readable date?
 
Similar Threads
Code review required for converting dateformat
String conversion
Really Challenging...HelpMe..java.Util - very Urgent
how to make different date format validation ?
Human Readable and Editable Object Input and Output Streams?