Avatar of tagomtech
tagomtech
Flag for United States of America asked on

calculating java.sql.timestamp

I need to calculate the amount of time that has elapsed between two dates using the java.sql.timestamp method -
variables - startTime is passed as a timestamp value and fillTime is passed as a timestamp value
I need the output to display as a double (i.e. startTime - fillTime = 1.48)
JavaJava EE

Avatar of undefined
Last Comment
tagomtech

8/22/2022 - Mon
ksivananth

fillTime.getTime().getTime() - startTime.getTime().getTime() will give you the milisec differences in double!
ASKER CERTIFIED SOLUTION
ksivananth

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
for_yan

why do you need twice to call getTime(), and getTime() returns long.
I'd think it would be

 double time_ms =  (double) (fillTime.getTime() - startTime.getTime());

see
http://download.oracle.com/javase/1.5.0/docs/api/java/sql/Timestamp.html

ksivananth

>>why do you need twice to call

that was a typo, see my last comment, its corrected!
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
tagomtech

ASKER
When I put this code into my expressions window for the variable and compile i get the following message

can not cast from long to timestamp

the gettime method returns a long

ksivananth

>>the gettime method returns a long

thats what you wanted?
tagomtech

ASKER
my bad, I had the return variable set to timestamp!
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
tagomtech

ASKER
When i try to use the convert statement

TimeUnit.Minutes.convert( fillTime.getTime() - startTime.getTime(), TimeUnit.MILLISECONDS )

to get the minutes it returns these errors
TimeUnit.Minutes cannot be resolved to a type
TimeUnit.MilliSeconds cannot be resolved to a type

how can i convert the milliseconds returned by this piece of code
fillTime.getTime() - startTime.getTime()
as it does indeed return the correct milliseconds.
for_yan

You can just cast long to double like that:

double time_ms =  (double) (fillTime.getTime() - startTime.getTime());
SOLUTION
CEHJ

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ksivananth

>>TimeUnit.Minutes cannot be resolved to a type

try
TimeUnit.MINUTES.convert( fillTime.getTime() - startTime.getTime(), TimeUnit.MILLISECONDS )
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
for_yan

getTime() - gives you value in milliseconds
double time_ms =  (double) (fillTime.getTime() - startTime.getTime());  

in you want in secnds:

double time_s =  ((double) (fillTime.getTime() - startTime.getTime()))/1000.0;  

if you want in minutes it will be:

double time_m =  ((double) (fillTime.getTime() - startTime.getTime()))/60000.0;

SOLUTION
for_yan

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
for_yan

If you really want in minutes and seconds (although 1.48 doesn't look like it is in minutes and seconds)
then you can do it like that:

double time_s =  ((double) (fillTime.getTime() - startTime.getTime()))/1000.0;  

int time_mins = (int) (time_s/60.0);

int time_sec = (int) (time_s - 60.0*time_mins);

So you'll write

System.out.println("  Time difference: " + time_mins + "'" + time_secs + "''  ");

You should get " Time difference 1'48''  "
Mick Barry

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
tagomtech

ASKER
@cehj yes it is a duplicate: I closed that questions out because I felt I had not appropriately asked the question to receive the help I needed....
Let me start over and explain where I am and what I am flustered over.
I am using iReports which has embedded classes or you can import classes
I am using ms sql as the database and CAN NOT change the query
I am trying to convert a crystal report to a iReport (jasperrepoert)
I do not need to write an entire class -  I need to be able to use what is given in iReport to configure the information

$V{responsetime} = $F{Crew Filled}.getTime() - $F{Call Started}.getTime() gives me the time as a long: in milliseconds
(($V{responsetime} % (1000*60*60)) / (1000*60)) gives me the correct minutes as a long and then I am casting to double with
$V{minutesResponseTime}.doubleValue() - this gives me the expected results

The issue comes into play when i am trying to get the average response time.
you would think that the minutesResponseTime/timescalled would give me the response time....it does not match with what crystal reports it giving me. SO
here is how crystal reports is figuring it"
 to find the minutes between time: (DateDiff ("s", {CC_Report_CrewHistory;1.Call Started}, {CC_Report_CrewHistory;1.Crew Filled}))/60 and we get the same results
to find the average time they are using a built in method that takes the @responsetime evaluates it per data column then uses the group crewname to reset the value.
that value theoretically should be the same in jasper reports but it is not
please see the pics to view the difference.....this is my roadblock and I just cannot see why there is a difference


jasperreoports.JPG
Mick Barry

> @cehj yes it is a duplicate: I closed that questions out because I felt I had not appropriately asked the question to receive the help I needed....

then you should delete it. Its confusing otherwise
and you're not permitted to open duplicate questions at EE.

tagomtech

ASKER
oops including other images now! crystal reports resultsto-seconds.JPG
configurationArea.JPG
Your help has saved me hundreds of hours of internet surfing.
fblack61
tagomtech

ASKER
I REALLY REALLY appreciate you guys helping me with this...
CEHJ

The first image with the yellow highlight looks like you HAVE used a double:

1.6666666666666666667 (or however many recurrences there are) is probably meant to be 1:40 or 1.40
tagomtech

ASKER
I just don't know. I can not see how the results are coming to the listed numbers.
In crystal reports that 1.67 is calculating to 1.89 (This has been why I have pushed the double variable - 1.89 is not minutes and seconds - neither is 1.66) Yes I was able to get it to convert to a double and I thought that would fix the calculation problem!
Any ideas.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
CEHJ

What exactly does that 1.67 mean?
tagomtech

ASKER
The 1.67 is the total time total minutes on the system (5) divided by the number of calls (3)
so the average of calls.
The strange thing is that in crystal reports I get the same number of minutes for each call as I do in my iReports BUT when the average is calculated in Crystal it is returning 1.89 and in iReports it is returning 1.67 the data is the same.
tagomtech

ASKER
puzzle solved -
in crystal reports the data was actually being figured as seconds to a 2 spot decimal then divided by 60
once i went back to seconds - formatted and then divided the reports matched -
i had to drill down in crystal to actually see why the data was being handled differently
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
CEHJ

>> the data was actually being figured as seconds to a 2 spot decimal then divided by 60

I'm not sure what's worse - a tenth of a minute, or a sixtieth of a second... ;)
tagomtech

ASKER
I know! these reports are just crazy!
I am getting ready to post another question of formatting iReports for weekly runs on the data.
I appreciate you sticking this out with me...as obvious I am having to learn a new program for this job - which seems to have a mind of its own.