How do you calculate elapsed number of days

A

Anjum

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,
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Anjum said:
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
 
A

Anjum

X-No-Archive: Yes
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.
 
L

Lew

Anjum said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top