Java String to date format

Discussion in 'Java' started by JD Uhler, Dec 30, 2003.

  1. JD Uhler

    JD Uhler Guest

    I want to convert the String "2001-09-07 00:00:00.0" into the format
    "09/07/2001". How do I do that without having to do a bunch of string
    manipulations? Can I use SimpleDateFormat to just convert it to a
    string? I have tried that, but an error comes back saying something
    like "Cannot convert Object to Date".

    The original String can be null as well.

    Thank you

    JD
    JD Uhler, Dec 30, 2003
    #1
    1. Advertising

  2. JD Uhler wrote:
    > I want to convert the String "2001-09-07 00:00:00.0" into the format
    > "09/07/2001". How do I do that without having to do a bunch of string
    > manipulations? Can I use SimpleDateFormat to just convert it to a
    > string? I have tried that, but an error comes back saying something
    > like "Cannot convert Object to Date".
    >
    > The original String can be null as well.
    >
    > Thank you
    >
    > JD



    Use a DateFormat to parse() this String to a Date object. Use a
    different DateFormat to format() the Date to the String representation
    you want.

    See http://www.joconner.com/javai18n/articles/DateFormat.html for more
    information.

    Regards,
    John O'Conner
    John O'Conner, Dec 30, 2003
    #2
    1. Advertising

  3. JD Uhler wrote:
    > I want to convert the String "2001-09-07 00:00:00.0" into the format
    > "09/07/2001". How do I do that without having to do a bunch of string
    > manipulations? Can I use SimpleDateFormat to just convert it to a
    > string? I have tried that, but an error comes back saying something
    > like "Cannot convert Object to Date".
    >
    > The original String can be null as well.
    >
    > Thank you
    >
    > JD



    Another (easier/quicker) option is to use String's split() method to
    create an array containing the substrings "2001", "09", and "07". Now
    you can create a new String using these substrings. There are a few
    string manipulations, but they are simple.

    Regards,
    John O'Conner
    John O'Conner, Dec 30, 2003
    #3
  4. JD Uhler

    JD Uhler Guest

    While we are on the topic of dates.. are there any methods/properties
    that tell you which fiscal year you are currently in? I was going to
    create the whole "greater/less than July 1st" routine, but thought
    there may be an easier way.

    Thank you

    JD

    John O'Conner <> wrote in message news:<FykIb.12158$>...
    > JD Uhler wrote:
    > > I want to convert the String "2001-09-07 00:00:00.0" into the format
    > > "09/07/2001". How do I do that without having to do a bunch of string
    > > manipulations? Can I use SimpleDateFormat to just convert it to a
    > > string? I have tried that, but an error comes back saying something
    > > like "Cannot convert Object to Date".
    > >
    > > The original String can be null as well.
    > >
    > > Thank you
    > >
    > > JD

    >
    >
    > Another (easier/quicker) option is to use String's split() method to
    > create an array containing the substrings "2001", "09", and "07". Now
    > you can create a new String using these substrings. There are a few
    > string manipulations, but they are simple.
    >
    > Regards,
    > John O'Conner
    JD Uhler, Dec 31, 2003
    #4
  5. JD Uhler

    P.Hill Guest

    JD Uhler wrote:
    > While we are on the topic of dates.. are there any methods/properties
    > that tell you which fiscal year you are currently in? I was going to
    > create the whole "greater/less than July 1st" routine, but thought
    > there may be an easier way.


    I assume you already are aware a the Calendar/GregorianCalendar class.

    -Paul
    P.Hill, Dec 31, 2003
    #5
  6. P.Hill wrote:
    >
    > I assume you already are aware a the Calendar/GregorianCalendar class.
    >
    > -Paul
    >




    Although Paul directs you to the Calendar class, which can help you add,
    subtract, and roll dates around, Calendar is unaware of domain specific
    terms like "fiscal year". You would have to create that class yourself,
    but certainly that class could use Calendar just as Paul suggests.


    --
    John
    John O'Conner, Dec 31, 2003
    #6
    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,159
    Janaka
    Feb 18, 2004
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,251
    Michael Borgwardt
    May 30, 2004
  3. Chris Angelico
    Replies:
    3
    Views:
    151
    Mark Lawrence
    Mar 1, 2013
  4. Peter Otten
    Replies:
    0
    Views:
    131
    Peter Otten
    Feb 28, 2013
  5. Rick Johnson
    Replies:
    0
    Views:
    138
    Rick Johnson
    Feb 28, 2013
Loading...

Share This Page