Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Converting from string to date Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Converting from string to date" Watch "Converting from string to date" New topic
Author

Converting from string to date

sam barker
Ranch Hand

Joined: Apr 26, 2008
Posts: 37
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Aug 21, 2007
Posts: 61
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

Joined: Apr 26, 2008
Posts: 37
"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
Marshal

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


Ping & DNS - my free Android networking tools app
Mintoo kumar
Ranch Hand

Joined: Aug 21, 2007
Posts: 61
_________________________________
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

Joined: Apr 26, 2008
Posts: 37
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Converting from string to date