Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   Problem Finding Difference Of 2 Longs (http://www.programmingforums.org/showthread.php?t=1210)

T3FLoN Nov 19th, 2004 4:20 PM

for skool they got me making a telephone witch is done and now my teacher added a phone bill to it...hes having us save the info of the number dialed time/date and duration of the call.

now im using the getTime() method witch returns the time in milliseconds since 1970 to now.
well i get both my longs witch are timeStart and timeEnd both get assigned a value as calls are made.
now i need to find the diferende of the 2 and store it in timeDuration witch is also a long.

my problem is that when i do this line.
:

durationTime = (timeEnd - timeStart);
its not giving me a correct answer just gives me timeEnd.
thats all the code ull need to give me a hand cause all else works as it should just not that equation.
BTW im Sys.out.printing them to see the values thats how i kno they are incorrect.
any insite would be appreciated.TX

Ooble Nov 19th, 2004 4:27 PM

Can we see the rest of the code?

sykkn Nov 19th, 2004 5:00 PM

If all of your values are indeed longs, then you should not be having a problem subtracting the two values.

:

import java.util.*;

class timetest {
    public static void main(String args[]) {
        long startStamp = new Date().getTime();

        try {
            Thread.sleep(500);
        } catch(Exception e) { }

        long endStamp = new Date().getTime();

        long diffStamp = (endStamp - startStamp);

        System.out.println("Start Stamp: " + startStamp);
        System.out.println("End Stamp: " + endStamp);
        System.out.println("Diff Stamp: " + diffStamp);
    }
}


:

jowings-pb:~/devel/java jowings$ java timetest
Start Stamp: 1100901620140
End Stamp: 1100901620640
Diff Stamp: 500


eccles Nov 21st, 2004 7:51 PM

pay careful attention to Sykkn's example.

note that he is creating a new Date instance each time he wants to get the curent time...

ie he is NOT doing:
:

Date date = new Date();
long timeStart = date.getTime();
...
long timeEnd = date.getTime();
long duration = timeEnd - timeStart;


which was my initial thought about what you might be doing, but if you are getting the end time instead of zero then perhaps you aren't.

but yeah - just look at that correct example and you might see where you went wrong.

oh - and if you don't know where you are going wrong with something - don't assume that all people with need to see is a single line of your code - one which clearly has nothing wrong with it :P


All times are GMT -5. The time now is 1:29 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC