Solved

calculating java.sql.timestamp

Posted on 2011-03-07
26
698 Views
Last Modified: 2013-11-23
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
Comment
Question by:tagomtech
  • 10
  • 5
  • 5
  • +2
26 Comments
 
LVL 26

Expert Comment

by:ksivananth
ID: 35057982
fillTime.getTime().getTime() - startTime.getTime().getTime() will give you the milisec differences in double!
0
 
LVL 26

Accepted Solution

by:
ksivananth earned 200 total points
ID: 35058005
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
 
LVL 47

Expert Comment

by:for_yan
ID: 35058043
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 26

Expert Comment

by:ksivananth
ID: 35058058
>>why do you need twice to call

that was a typo, see my last comment, its corrected!
0
 

Author Comment

by:tagomtech
ID: 35058409
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
 
LVL 26

Expert Comment

by:ksivananth
ID: 35058428
>>the gettime method returns a long

thats what you wanted?
0
 

Author Comment

by:tagomtech
ID: 35058539
my bad, I had the return variable set to timestamp!
0
 

Author Comment

by:tagomtech
ID: 35058835
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
 
LVL 47

Expert Comment

by:for_yan
ID: 35059062
You can just cast long to double like that:

double time_ms =  (double) (fillTime.getTime() - startTime.getTime());
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 200 total points
ID: 35059521
>>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
 
LVL 26

Expert Comment

by:ksivananth
ID: 35059553
>>TimeUnit.Minutes cannot be resolved to a type

try
TimeUnit.MINUTES.convert( fillTime.getTime() - startTime.getTime(), TimeUnit.MILLISECONDS )
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35059598
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
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 100 total points
ID: 35059619
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
 
LVL 47

Expert Comment

by:for_yan
ID: 35059764
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
 
LVL 92

Expert Comment

by:objects
ID: 35061469
0
 

Author Comment

by:tagomtech
ID: 35061527
@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
 
LVL 92

Expert Comment

by:objects
ID: 35061539
> @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
 

Author Comment

by:tagomtech
ID: 35061543
oops including other images now! crystal reports resultsto-seconds.JPG
configurationArea.JPG
0
 

Author Comment

by:tagomtech
ID: 35061555
I REALLY REALLY appreciate you guys helping me with this...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35062074
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
 

Author Comment

by:tagomtech
ID: 35070063
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 35070190
What exactly does that 1.67 mean?
0
 

Author Comment

by:tagomtech
ID: 35070288
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
 

Author Closing Comment

by:tagomtech
ID: 35083807
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 35083875
>> 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
 

Author Comment

by:tagomtech
ID: 35083917
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

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to implement Singleton Design Pattern in Java.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question