English 中文(简体)
计算 Java开端和终点之间的秒数?
原标题:Calculate the amount of seconds between start and end in Java?
  • 时间:2011-10-12 19:53:22
  •  标签:
  • java

我试图完成我必须完成的任务,即在像种族这样的艰难时刻计算第二秒。 在格式上打上头盔。 第二次计算结果为时数和秒。 这是非目标方案拟订的开端课程,因此,我使用的守则必须是简单的,没有额外的班级来处理时间。 我之所以要问,是因为我的计算有时得出错误的结果。 我这样做了:

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

我利用Math.abs去除负面数字。

得出总数额,我增加所有变量。 最后,我这样说:

resultHour = totalAmountOfSeconds / 3600;
rest totalAmountOfSeconds % 3600;

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

当我起步时,如1 50030,最后时间为165015,结果为1小时50分15秒。 如果是45秒,我会认为应该更加正确吗? 我的计算可能是错误的? 是否有更好的办法,或者我是否可以修改或简化我的准则,从而产生正确的结果? 给予一些帮助! 感谢!

最佳回答

这是因为你使用Math.abs()

让我举一个小例子: 时间从00:00:59开始,2秒在0:01:01时结束

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

因此,第60+58 = 118秒而不是2秒。 如果我们去除Math.abs,我们就有:

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

60 + (-58) = 2秒(如 su)。

问题回答

您的二人基本上都在做事。

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

因此,你的问题是。

你也许想试图让国际自由论坛尝试二者是消极的。

以起步时间为例:1:10:10和结尾时间1:11:05显然区别应为55秒。

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

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





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签