How do you calculate elapsed number of days

Discussion in 'Java' started by Anjum, Dec 14, 2006.

  1. Anjum

    Anjum Guest

    X-No-Archive: Yes

    I retrieve date from mySQL using ResultSet.getDate("<field name>")
    metbod. How do I calculate how many days have elapsed between that date
    and today's date.

    For example,

    ResultSet rs = ...
    rs.getDate("<field name");

    Please note that I strictly use JSP and not Java classes.

    Thanks,
     
    Anjum, Dec 14, 2006
    #1
    1. Advertising

  2. Anjum wrote:
    > I retrieve date from mySQL using ResultSet.getDate("<field name>")
    > metbod. How do I calculate how many days have elapsed between that date
    > and today's date.


    Something like:

    (rs.getDate("fieldname").getTime() - (new Date()).getTime())/(24 * 60 *
    60 * 1000L)

    I would assume.

    Arne
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Dec 15, 2006
    #2
    1. Advertising

  3. Anjum

    Anjum Guest

    X-No-Archive: Yes

    Arne Vajhøj wrote:
    > Anjum wrote:
    > > I retrieve date from mySQL using ResultSet.getDate("<field name>")
    > > metbod. How do I calculate how many days have elapsed between that date
    > > and today's date.

    >
    > Something like:
    >
    > (rs.getDate("fieldname").getTime() - (new Date()).getTime())/(24 * 60 *
    > 60 * 1000L)
    >
    > I would assume.
    >


    Thanks, it did not work due to a conflict between the Date object
    obtained from RS and the Date() object. However, the error message I
    received and your own technique above gave me enough clues to
    experiment and the following worked:

    long theTime = System.currentTimeMillis();
    java.sql.Date theDate = new Date(theTime);

    long noOfDaysElapsed = 0;
    noOfDaysElapsed = (theDate.getTime() - rs.getDate("<field
    name>").getTime())/(24 * 60 * 60 * 1000L);

    Thanks for your assistance.
     
    Anjum, Dec 16, 2006
    #3
  4. Anjum

    Lew Guest

    Anjum wrote:
    > java.sql.Date theDate = new Date(theTime);


    That this compiled indicates that you have imported "java.sql.Date" and not
    "java.util.Date". In that case you are not absolutely required to specify the
    package name in the type declaration of "theDate", although of course it does
    no harm whatsoever. OTOH, one could argue that by showing the package you are
    enhancing the self-documentation of the code by emphasizing that this is the
    "sql" version of Date. I believe I will adopt this idiom where it enhances
    understanding in that fashion.

    - Lew
     
    Lew, Dec 16, 2006
    #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. David Stockwell
    Replies:
    2
    Views:
    703
    Anna Martelli Ravenscroft
    Sep 20, 2004
  2. Gerrit
    Replies:
    0
    Views:
    388
    Gerrit
    Sep 20, 2004
  3. chrismo
    Replies:
    2
    Views:
    181
    Phrogz
    Jan 16, 2007
  4. rutherf
    Replies:
    2
    Views:
    432
    rutherf
    Oct 28, 2006
  5. Luca Villa
    Replies:
    2
    Views:
    151
    Luca Villa
    Nov 11, 2007
Loading...

Share This Page