Convert a String to Date object with format

Discussion in 'Java' started by Alex, Jul 4, 2003.

  1. Alex

    Alex Guest

    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
     
    Alex, Jul 4, 2003
    #1
    1. Advertising

  2. Alex wrote:
    > 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.
     
    David Zimmerman, Jul 4, 2003
    #2
    1. Advertising

  3. Alex

    Jacob Guest

    David Zimmerman wrote:
    >
    >
    > Alex wrote:
    >> SimpleDateFormat fmt = new SimpleDateFormat("MM'/'dd'/'yy");


    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.
     
    Jacob, Jul 4, 2003
    #3
  4. Just use the format object to format the date back to a string:

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

    Allan Wisborg
     
    Allan Wisborg, Jul 4, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Candy
    Replies:
    2
    Views:
    1,194
    Janaka
    Feb 18, 2004
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,327
    Michael Borgwardt
    May 30, 2004
  3. wgan
    Replies:
    7
    Views:
    610
    Roedy Green
    Jul 8, 2004
  4. Replies:
    8
    Views:
    804
    Boris
    May 1, 2007
  5. amith
    Replies:
    2
    Views:
    108
    Dr John Stockton
    Feb 11, 2004
Loading...

Share This Page