Convert a String to Date object with format

A

Alex

Hello everyone,
I'm having a problem with the Date object and I need an example. I
have a string, say for example, 01/31/98.I parse that string to a date
object and As a Date object that shows:

CODE:
SimpleDateFormat fmt = new SimpleDateFormat("MM'/'dd'/'yy");
Date date = fmt.parse("01/31/98");
System.out.println(date);

OUTPUT:
Sat Jan 31 00:00:00 EST 1998

I don't want the "00:00:00 EST" when i do a System.out.println of that
date object. I mean i could always parse it
but i'd be left with a string version of the date object, when really
I want the sole Date returned without the "00:00:00 EST" as of type
Date, and not String. Once the "00:00:00 EST" is gone, if i do
System.out.println(dateobject), it should print out just "Sat Jan 31
1998" Is there anyone out there with suggestions? Thanks a lot.

Alex
 
D

David Zimmerman

Alex said:
Hello everyone,
I'm having a problem with the Date object and I need an example. I
have a string, say for example, 01/31/98.I parse that string to a date
object and As a Date object that shows:

CODE:
SimpleDateFormat fmt = new SimpleDateFormat("MM'/'dd'/'yy");
Date date = fmt.parse("01/31/98");
System.out.println(date);

OUTPUT:
Sat Jan 31 00:00:00 EST 1998

you parsed it perfectly, now you have a date object with the actual date
encoded inside in some magic way (actually, it's just a long counting
milliseconds since Jan 1, 1970 00:00:00 UTC. Then you printed it with
the default toString(). The default is as you see it. The date can be
formated for output just as easily as it was parsed using a DateFormat.
 
J

Jacob

As a matter of style, this is better:

DateFormat fmt = new SimpleDateFormat("MM'/'dd'/'yy");

It is equivalent to the common:

Collection c = new ArrayList();

etc.
 
A

Allan Wisborg

Just use the format object to format the date back to a string:

System.out.println(fmt.format(date));

Allan Wisborg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top