• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 772
  • Last Modified:

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)
0
tagomtech
Asked:
tagomtech
  • 10
  • 5
  • 5
  • +2
3 Solutions
 
ksivananthCommented:
fillTime.getTime().getTime() - startTime.getTime().getTime() will give you the milisec differences in double!
0
 
ksivananthCommented:
if you want the differences in diff unit, use TimeUnit, for e.g., below gives the differences in hour,

TimeUnit.HOURS.convert( fillTime.getTime() - startTime.getTime(), TimeUnit.MILLISECONDS ) ;

and the above post should be

fillTime.getTime() - startTime.getTime() will give you the milisec differences in double!
0
 
for_yanCommented:
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

0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ksivananthCommented:
>>why do you need twice to call

that was a typo, see my last comment, its corrected!
0
 
tagomtechAuthor Commented:
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

0
 
ksivananthCommented:
>>the gettime method returns a long

thats what you wanted?
0
 
tagomtechAuthor Commented:
my bad, I had the return variable set to timestamp!
0
 
tagomtechAuthor Commented:
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.
0
 
for_yanCommented:
You can just cast long to double like that:

double time_ms =  (double) (fillTime.getTime() - startTime.getTime());
0
 
CEHJCommented:
>>I need the output to display as a double (i.e. startTime - fillTime = 1.48)

This is  essentially a duplicate of your other question (which i've already answered). As i mentioned there, a double is not relevant: you want a display in minutes and seconds. Time is not decimal

See http://technojeeves.com/joomla/index.php/free/71-difference-between-two-dates-in-java
long[] diffs = TimeDiff.getTimeDifference(timestamp1, timestamp2);
String minSec = String.format("%d.%d", diffs[2], diffs[3]);

Open in new window

0
 
ksivananthCommented:
>>TimeUnit.Minutes cannot be resolved to a type

try
TimeUnit.MINUTES.convert( fillTime.getTime() - startTime.getTime(), TimeUnit.MILLISECONDS )
0
 
for_yanCommented:
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;

0
 
for_yanCommented:
If you want with two digits after decimal pioints you can use format like that:

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

 NumberFormat formatter = new DecimalFormat("#0.00");

System.out.println("The Decimal Value is:"+formatter.format(time_m));

0
 
for_yanCommented:
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''  "
0
 
objectsCommented:
0
 
tagomtechAuthor Commented:
@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
0
 
objectsCommented:
> @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.

0
 
tagomtechAuthor Commented:
oops including other images now! crystal reports resultsto-seconds.JPG
configurationArea.JPG
0
 
tagomtechAuthor Commented:
I REALLY REALLY appreciate you guys helping me with this...
0
 
CEHJCommented:
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
0
 
tagomtechAuthor Commented:
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.
0
 
CEHJCommented:
What exactly does that 1.67 mean?
0
 
tagomtechAuthor Commented:
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.
0
 
tagomtechAuthor Commented:
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
0
 
CEHJCommented:
>> 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... ;)
0
 
tagomtechAuthor Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 10
  • 5
  • 5
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now