3D-kreativ
3D-kreativ

Reputation: 9299

Calculate the amount of seconds between start and end in Java?

I'm trying to solve a task that I have to do that is to calculate the amount of seconds between a star time and a end time like a race. In the format hhmmss. With the amount of seconds finally calculate the result in hours minutes and seconds. This is a beginners course with non-objective programming, so the code I use must be simple and without any extra classes to handle time. The reason why I ask is because my calculation sometimes calculate a wrong result. I have done it like this:

totalAmountOfSecondsPerHour = (endHour - startHour) * 3600;
totalAmountOfMinutesPerMinutes = Math.abs((endMinutes - startMinutes) * 60);
totalAmountOfSeconds = Math.abs(endSeconds - startSeconds));

I use Math.abs to get rid of negative numbers.

To get the total amount in seconds I add all variables. Finally to get the result time in hhmmss I do like this:

resultHour = totalAmountOfSeconds / 3600;
rest totalAmountOfSeconds % 3600;

resultMinutes = rest / 60;
resultSeconds = rest % 60;

When I enter a starting time like 150030 and end time like 165015 the result is 1 hour 50 minutes an 15 seconds. I guess i should be more correct if it was 45 seconds!? What could be wrong in my calculation? Is there a better way or could I modify or simplify my code the make a correct result? Preciate some help! Thanks!

Upvotes: 1

Views: 392

Answers (4)

hdk
hdk

Reputation: 13

Those Math.abs tripped you up... Convert the time to seconds, do substraction and then convert back to hhmmss format.

Upvotes: 0

Jens Schauder
Jens Schauder

Reputation: 81907

Take the example of start time: 1:10:10 and end time 1:11:05 obviously the difference should be 55 seconds.

But you do abs(5-10) for the seconds and get 5 seconds. And you get 1 minute 5 seconds.

You have two approaches to fix this: Get rid of the abs and instead if you get a negative result add that (negative) amount to the amount of the next 'higher' value.

So in the example instead of adding abs(-5) to 1 minute you add -5 to 1 minute, resulting in 0 minutes and 55 seconds.

While this will work it will get quite confusing.

A better approach would be: convert start and end time into seconds. Take the difference. Convert the result back into hours, minutes and seconds

Upvotes: 1

Nettogrof
Nettogrof

Reputation: 2136

It's because you use the Math.abs()

let give you a small example: The app start at 00:00:59 and finish 2 seconds later so at 00:01:01

totalAmouneOfMinutesPerMinutes = Math.abs(1 - 0) * 60  = 60
totalAmountOfSeconds = Math.abs(1 - 59) = 58   

so 60 + 58 = 118 seconds instead of 2. If we remove the Math.abs, we'll have:

totalAmouneOfMinutesPerMinutes = (1 - 0) * 60  = 60
totalAmountOfSeconds = (1 - 59) = -58   

60 + (-58) = 2 seconds (like it's suppose).

Upvotes: 3

Louis Ricci
Louis Ricci

Reputation: 21086

For your seconds calc you are essentially doing

15 - 30 = -15 ABS(-15) = 15

So there's your problem.

You may want to try IF the seconds are negative THEN add 60.

Upvotes: 2

Related Questions