Difference between time

goon652001
goon652001 used Ask the Experts™
on
I had a compilation error when I did this;

     
        Calendar stop = Calendar.getInstance();
        a = start.getTime();//got earlier before this
        stopTime = stop.getTime();    
       total = stopTime -  a;
       
ERROR:operator - cannot be applied to  
                 java.util.Date,java.util.Date
             total = stopTime -  a;

How do I get the difference in sec?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
do this:
long lStopTime = stopTime.getTime();
long lA = a.getTime();

total = new Date(lStopTime - lA);

-gkern
thats because Calendar getTime() returns a Date Object, Date getTime() return the long value of the Date object.
-gkern
last thing:
operators in java don't work between objects, operators work only between int/double/long/char/float etc.
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

>> How do I get the difference in sec ?
long lStopTime = stopTime.getTime();
long lA = a.getTime();

long diff = (lStopTime - lA)/1000; // from milisec to sec

Commented:
Presuming "a" is a Date object, do this...


Calendar stop = Calendar.getInstance();
a = start.getTime();//got earlier before this
stopTime = stop.getTime().getTime();    
total = (stopTime -  a.getTime())/1000;

vemul

Author

Commented:
Thanks for the response...
I will try it later....
I appreciate it

consider trying this

Calendar stop = Calendar.getInstance();
long a = start.getTimeInMillis();
long stopTime = stop.getTimeInMillis();
long total = (stopTime - a)/1000;

if u wan total to be an integer .. u can cast it to int total = (int)((stopTime - a)/1000)

but i dun see a need in that
hehehe
hope it will work for u

:)
-=[sKySh@DoW]=-

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial