Lost in "Date" maze

Discussion in 'Java' started by Tim Slattery, Jan 16, 2004.

  1. Tim Slattery

    Tim Slattery Guest

    I'm retrieving a datetime value from my database. I can easily
    retrieve this into a java.sql.Date object. Once retrieved, I want to
    display the month and year from it in a yyyymm format. I also want to
    calculate the four preceding months, and display them likewise. So if
    I retrieve a date in September, 2003 from the DB, I want the following
    five display strings: 200309, 200308, 200307, 200306, 200305.

    So I can use Date.getYear() and Date.getMonth(), then manipulate them,
    right? But those functions are deprecated, and we're supposed to use
    Calendar.get(Calendar.YEAR) and Calendar.get(Calendar.MONTH).

    Ok...Calendar includes a neat add(...) function that I can use to do
    date arithmetic. But Calendar.get(Calendar.MONTH) returns one of the
    constants defined within Calendar, and they could have any value; so
    how do I format the string that I need? Hmm..there's a DateFormat
    class....but I can't for the life of me figure out how to make it
    yield the string that I need.

    So what's the answer? Should I just give up and use the deprecated
    functions from java.util.Date (which java.sql.Date extends)?

    --
    Tim Slattery
     
    Tim Slattery, Jan 16, 2004
    #1
    1. Advertising

  2. "Tim Slattery" <> wrote in message
    news:p...
    > I'm retrieving a datetime value from my database. I can easily
    > retrieve this into a java.sql.Date object. Once retrieved, I want to
    > display the month and year from it in a yyyymm format. I also want to
    > calculate the four preceding months, and display them likewise. So if
    > I retrieve a date in September, 2003 from the DB, I want the following
    > five display strings: 200309, 200308, 200307, 200306, 200305.
    >
    > So I can use Date.getYear() and Date.getMonth(), then manipulate them,
    > right? But those functions are deprecated, and we're supposed to use
    > Calendar.get(Calendar.YEAR) and Calendar.get(Calendar.MONTH).
    >
    > Ok...Calendar includes a neat add(...) function that I can use to do
    > date arithmetic. But Calendar.get(Calendar.MONTH) returns one of the
    > constants defined within Calendar, and they could have any value; so
    > how do I format the string that I need? Hmm..there's a DateFormat
    > class....but I can't for the life of me figure out how to make it
    > yield the string that I need.


    You're very close to the answer. You want SimpleDateFormat. This class
    lets you express the format of a date as a string. The API shows how to use
    the formatting characters and gives some examples. To get the 200309 format
    for September 2003, you would use

    SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMM");
    String s = sdf.format (date);

    Other formats can be like MM-dd-yyyy HH:mm:ss:SS" for the normal numeric
    date of 09-01-2004 05:13:12:78. Using longer format expressions (e.g. MMMM
    instead of MM) will insert the word rather than the number.

    There are also some nice localization features in the API, such as
    DateFormat df = DateFormat.getDateTimeInstance();
    String s = df.format( new Date () );

    So that different localities get different formats.


    Cheers,
    Matt Humphrey http://www.iviz.com/
     
    Matt Humphrey, Jan 16, 2004
    #2
    1. Advertising

  3. Tim Slattery

    kaeli Guest

    In article <>,
    enlightened us with...
    >
    > So what's the answer? Should I just give up and use the deprecated
    > functions from java.util.Date (which java.sql.Date extends)?
    >


    No.

    java.sql.Date myDB_date = rs.getDate("myDB_field");
    java.text.SimpleDateFormat df = new java.text.SimpleDateFormat
    ("yyyyMM");
    String myString = df.format(myDB_date);

    You get your format.

    df.format takes a date.

    Add your dates as calendars, which you figured out how to do from what I
    can tell. Then do
    myString = df.format(myCalendar.getTime());
    Calendar.getTime() returns a date. SimpleDateFormat.format takes one.

    --
    --
    ~kaeli~
    Going to church doesn't make you a Christian any more than
    standing in a garage makes you a car.
    http://www.ipwebdesign.net/wildAtHeart
    http://www.ipwebdesign.net/kaelisSpace
     
    kaeli, Jan 16, 2004
    #3
  4. Tim Slattery

    Tim Slattery Guest

    "Matt Humphrey" <> wrote:

    >
    >"Tim Slattery" <> wrote in message
    >news:p...
    >> I'm retrieving a datetime value from my database. I can easily
    >> retrieve this into a java.sql.Date object. Once retrieved, I want to
    >> display the month and year from it in a yyyymm format. I also want to
    >> calculate the four preceding months, and display them likewise. So if
    >> I retrieve a date in September, 2003 from the DB, I want the following
    >> five display strings: 200309, 200308, 200307, 200306, 200305.
    >>
    >> So I can use Date.getYear() and Date.getMonth(), then manipulate them,
    >> right? But those functions are deprecated, and we're supposed to use
    >> Calendar.get(Calendar.YEAR) and Calendar.get(Calendar.MONTH).
    >>
    >> Ok...Calendar includes a neat add(...) function that I can use to do
    >> date arithmetic. But Calendar.get(Calendar.MONTH) returns one of the
    >> constants defined within Calendar, and they could have any value; so
    >> how do I format the string that I need? Hmm..there's a DateFormat
    >> class....but I can't for the life of me figure out how to make it
    >> yield the string that I need.

    >
    >You're very close to the answer. You want SimpleDateFormat. This class
    >lets you express the format of a date as a string. The API shows how to use
    >the formatting characters and gives some examples. To get the 200309 format
    >for September 2003, you would use
    >
    >SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMM");
    >String s = sdf.format (date);


    OK, thanks. But to do arithmetic on a date, I have to create a
    Calendar object from it. Can I use SimpleDateFormat with a Calendar
    object?

    --
    Tim Slattery
     
    Tim Slattery, Jan 16, 2004
    #4
  5. In article <>,
    Tim Slattery <> wrote:

    >:"Matt Humphrey" <> wrote:
    >:
    >:>
    >:>"Tim Slattery" <> wrote in message
    >:>news:p...
    >:>> I'm retrieving a datetime value from my database. I can easily
    >:>> retrieve this into a java.sql.Date object. Once retrieved, I want to
    >:>> display the month and year from it in a yyyymm format. I also want to
    >:>> calculate the four preceding months, and display them likewise. So if
    >:>> I retrieve a date in September, 2003 from the DB, I want the following
    >:>> five display strings: 200309, 200308, 200307, 200306, 200305.
    >:>>
    >:>> So I can use Date.getYear() and Date.getMonth(), then manipulate them,
    >:>> right? But those functions are deprecated, and we're supposed to use
    >:>> Calendar.get(Calendar.YEAR) and Calendar.get(Calendar.MONTH).
    >:>>
    >:>> Ok...Calendar includes a neat add(...) function that I can use to do
    >:>> date arithmetic. But Calendar.get(Calendar.MONTH) returns one of the
    >:>> constants defined within Calendar, and they could have any value; so
    >:>> how do I format the string that I need? Hmm..there's a DateFormat
    >:>> class....but I can't for the life of me figure out how to make it
    >:>> yield the string that I need.
    >:>
    >:>You're very close to the answer. You want SimpleDateFormat. This class
    >:>lets you express the format of a date as a string. The API shows how to use
    >:>the formatting characters and gives some examples. To get the 200309 format
    >:>for September 2003, you would use
    >:>
    >:>SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMM");
    >:>String s = sdf.format (date);
    >:
    >:OK, thanks. But to do arithmetic on a date, I have to create a
    >:Calendar object from it. Can I use SimpleDateFormat with a Calendar
    >:eek:bject?
    >:
    >:--
    >:Tim Slattery
    >:


    You can create a Calendar in more than one way (like the static call to
    Calendar.getInstance(), or a new GregorianCalendar()), then set its
    fields by calling setTime() and passing your Date object. Then the
    SimpleDateFormat class has a setCalendar() method which will accept your
    Calendar as a parameter. This essentially has to do with locales and
    such, and not the date/time to be formatted, so you may not need it.

    = Steve =
    --
    Steve W. Jackson
    Montgomery, Alabama
     
    Steve W. Jackson, Jan 16, 2004
    #5
  6. Tim Slattery

    P.Hill Guest

    Tim,


    Steve W. Jackson wrote:
    > You can create a Calendar in more than one way (like the static call to
    > Calendar.getInstance(), or a new GregorianCalendar()), then set its
    > fields by calling setTime() and passing your Date object. Then the
    > SimpleDateFormat class has a setCalendar() method which will accept your
    > Calendar as a parameter. This essentially has to do with locales and
    > such, and not the date/time to be formatted, so you may not need it.


    You can do a SimpleDateFormat.getCalendar()/setCalendar(). It will return you
    the calendar it is using and not a copy, but there is no way, or at least there
    didn't used to be any way, to get a SimpleDateFormat to read not from a new Date
    going in, but from a new Calendar going into the format.

    So you might as well not violate any pedantic OO rules by trying to manipulate
    a Calendar that is being used by a DateFormat, but instead use one Calendar to
    play with the date, then do a myCal.getTime() and push that into
    DateFormat.format( date ) for a nice String.
    That is what I would do.

    Make sure if you do have to deal with TZ etc. in your calculating Calendar
    you also set the TZ in the formatter (or the underling calendar), otherwise
    you may really confuse yourself when the results are not what you expect.

    -Paul
     
    P.Hill, Jan 16, 2004
    #6
  7. Ok, but How can I use a JFormattedTextField?
    It accepts only Date class... :-(
    Giorgio

    P.Hill wrote:

    > Tim,
    >
    >
    > Steve W. Jackson wrote:
    >
    >> You can create a Calendar in more than one way (like the static call
    >> to Calendar.getInstance(), or a new GregorianCalendar()), then set its
    >> fields by calling setTime() and passing your Date object. Then the
    >> SimpleDateFormat class has a setCalendar() method which will accept
    >> your Calendar as a parameter. This essentially has to do with locales
    >> and such, and not the date/time to be formatted, so you may not need it.

    >
    >
    > You can do a SimpleDateFormat.getCalendar()/setCalendar(). It will
    > return you
    > the calendar it is using and not a copy, but there is no way, or at
    > least there didn't used to be any way, to get a SimpleDateFormat to read
    > not from a new Date going in, but from a new Calendar going into the
    > format.
    >
    > So you might as well not violate any pedantic OO rules by trying to
    > manipulate
    > a Calendar that is being used by a DateFormat, but instead use one
    > Calendar to
    > play with the date, then do a myCal.getTime() and push that into
    > DateFormat.format( date ) for a nice String.
    > That is what I would do.
    >
    > Make sure if you do have to deal with TZ etc. in your calculating Calendar
    > you also set the TZ in the formatter (or the underling calendar), otherwise
    > you may really confuse yourself when the results are not what you expect.
    >
    > -Paul
    >
     
    Giorgio Franceschetti, Jan 19, 2004
    #7
  8. Giorgio Franceschetti wrote:
    > Ok, but How can I use a JFormattedTextField?
    > It accepts only Date class... :-(


    The answer has already been given (implicitly) in this thread.

    kaeli wrote:
    > Calendar.getTime() returns a date.
     
    Thomas Schodt, Jan 19, 2004
    #8
    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. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,361
    Michael Borgwardt
    May 30, 2004
  2. Roger Douglass

    C++ Maze Program

    Roger Douglass, Jun 29, 2003, in forum: C++
    Replies:
    2
    Views:
    19,937
    marko djogatovic
    Jul 2, 2003
  3. Bernard Fields

    Making a maze....

    Bernard Fields, Nov 13, 2003, in forum: Python
    Replies:
    22
    Views:
    1,419
    Andrew Dalke
    Nov 25, 2003
  4. Daniel
    Replies:
    4
    Views:
    396
    Barry Schwarz
    Nov 22, 2004
  5. yb
    Replies:
    3
    Views:
    566
    Victor Bazarov
    Dec 9, 2005
Loading...

Share This Page