Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Converting from string to date

 
sam barker
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to convert a string into a date object

eg
string x="2008-05-04";
Date date1=new SimpleDateFormat("yyyy-MM-dd").parse(x);

but then date contains the value Sun May 04 00:00:00 EST 2008
I want to date1 to contain the date as 2008-05-04
How can I do this?
Cheers,
Sam
 
Paul Clapham
Sheriff
Pie
Posts: 20719
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They are the same thing, aren't they? Bear in mind that a Date is not a String. A Date is just a number of milliseconds from a certain instant of time, that's all.

What you are looking at when you see "Sun May 04 00:00:00 EST 2008" is simply a string representation of the Date. It isn't the Date itself, which is just that number of milliseconds. And if you want a different string representation, then you use that same SimpleDateFormat object to format it as a String.
 
Mintoo kumar
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First you format you "x" with ,Date.valueOf() . which will give you string and then format your date1.

______________________
Mintoo
SCJP 1.4
______________________
 
sam barker
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"First you format you "x" with ,Date.valueOf() . which will give you string and then format your date1."

I didnt understand.Could you give me an example.

This issue has been taking most of my time
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sam, I think you'll have more luck following the suggestion Paul posted. It's the stable approach to this problem (and the SimpleDateFormat class is very useful to know).
 
Mintoo kumar
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
_________________________________
date1.format(Date.valueOf(x))
_____________________________________

Besides that as every buddy is suggesting explore simpleDateFormat which will give the clear picture of how to manipulat with the dateformat.

________________________
Mintoo
SCJP 1.4
__________________________
 
sam barker
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help guys.But I am still stuck.

I did check out the SimpleDateFormat class

But when you phase a string with the format "yyyy-MM-dd" shouldnt yu get the
date in "yyyy-MM-dd" format ?

@Mintoo,
I had tried uot you solution its not working.
 
Paul Clapham
Sheriff
Pie
Posts: 20719
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sam barker:
But when you phase a string with the format "yyyy-MM-dd" shouldnt yu get the date in "yyyy-MM-dd" format ?.
No. You didn't understand what I originally said. Dates aren't ever "in a format". Date doesn't have a concept of "format" in any way. The idea of a Date being "in a format" doesn't mean anything.

If you just do System.out.println(someDate), that prints a String representation of the Date object using the standard format for Date objects. That's probably what you're seeing. The Date object doesn't remember how it was created, it's just a Date, that's all. If you want to display a differently-formatted String representation of the Date, well, we went through that already.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic