• Post Reply Bookmark Topic Watch Topic
  • New Topic

Convert a string to date  RSS feed

 
Lara Quinn
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I know how to convert a string to date by using dateformat.parse() method.
But if my input string is like "20120102", and how can I convert it to a format "yyyy-mm-dd"?

thanks in advance!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lara Quinn wrote:But if my input string is like "20120102", and how can I convert it to a format "yyyy-mm-dd"?

That, surely, is not a date conversion, but a simple String conversion, no?

That said, Strings that contain dates, are usually BAD.

Read the StringsAreBad page if you don't believe me.

Winston
 
Lara Quinn
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Lara Quinn wrote:But if my input string is like "20120102", and how can I convert it to a format "yyyy-mm-dd"?

That, surely, is not a date conversion, but a simple String conversion, no?

That said, Strings that contain dates, are usually BAD.

Read the StringsAreBad page if you don't believe me.

Winston


Yes, I agree. But this string is coming from third party, and it represents a date, so I have to convert it to Date.
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
That said, Strings that contain dates, are usually BAD.


A brave thing to say so loud in these web service ridden times ;-)
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lara Quinn wrote:Yes, I agree. But this string is coming from third party, and it represents a date, so I have to convert it to Date.

Erm, not from what you said in your first post you don't.

You simply have to convert it into a slightly different "date-like-string". And that is simply a String conversion.

Winston
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Jozsef Balazs wrote:A brave thing to say so loud in these web service ridden times ;-)

Yup. One of the great drawbacks of our ML times.

Winston
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you want to convert "20120102" to "2012-01-02"?

If that's what you want then Winston is right, it's a very simple piece of code involving only Strings. But perhaps you need a Date object for some other purpose? You haven't said what you need the proposed Date for, and I notice that very often, beginners get confused between Date objects and their String representations.
 
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
I hope that you don't mean "I want a Date object in the format 'yyyy-MM-dd'". Date objects do not have a format by themselves. They just contain a date and time value, and no information about the format.

See our JavaDatesFaq for more information on formatting and parsing Date objects.
 
Lara Quinn
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys, and I got it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!