Question

Can anyone assist me as to why my seconds are not printing out correctly? The hours and minutes are both correct, but it is not printing out anything for the secs variable. The items in my arraylist are in System.currentTimeMillis(), which is why I have to convert them.

//divides 'logoutTimeTotal' by 'loginTimeTotal' to get 'totalTimeLoggedIn'
long totalTimeLoggedIn = (((totalTimeArray.get(1) - totalTimeArray.get(0)) / 1000) / 60);

//creates 2 variables that convert time logged in to hours and minutes
long hr = totalTimeLoggedIn / 60;
long min = totalTimeLoggedIn - (hr * 60);
long secs = totalTimeLoggedIn;

//creates String variable with hours and minutes logged in
String timeLoggedStr = hr + " Hours " + min + " Minutes " + secs + " Seconds";

Thanks

Was it helpful?

Solution

You problem is when you getting the totalTimeLoggedIn, you do:

long totalTimeLoggedIn = (((totalTimeArray.get(1) - totalTimeArray.get(0)) / 1000) / 60);

If you want to get the total time in second that generating using System.currentTimeMillis(), then you have to divide just on 1000,since the 1 second is 1000 milliseconds, so you need to do:

long totalTimeLoggedIn = (((totalTimeArray.get(1) - totalTimeArray.get(0)) / 1000));

OTHER TIPS

It is probably caused by rounding.

Instead of long, using float is better.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top