Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How do you calculate elapsed number of days

Reply
Thread Tools

How do you calculate elapsed number of days

 
 
Anjum
Guest
Posts: n/a
 
      12-14-2006
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,

 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Arne_Vajh=F8j?=
Guest
Posts: n/a
 
      12-15-2006
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
 
Reply With Quote
 
 
 
 
Anjum
Guest
Posts: n/a
 
      12-16-2006
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.

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      12-16-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
calculate the number of days between two dates Luca Villa Perl Misc 2 11-11-2007 11:51 AM
Calculate number of non-weekend days between two dates? chrismo Ruby 2 01-16-2007 09:04 PM
Date::Manip hours elapsed not business hours elapsed rutherf Perl Misc 2 10-28-2006 08:05 AM
Re: 3-day novice, How do I calculate age in days? [C++] Alf P. Steinbach C++ 7 03-27-2005 02:35 PM



Advertisments