Java 以十进制格式转换小时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24746645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 14:39:20  来源:igfitidea点击:

Converting hours in decimal format

java

提问by KyleCrowley

I've tried mutliple solutions to this problem but I can't seem to get it.

我已经尝试了多种解决方案来解决这个问题,但似乎无法解决。

I have time in decimal format, which is in hours. I want to make it much cleaner by changing it into a DD:HH:MM:SS format.

我有十进制格式的时间,以小时为单位。我想通过将其更改为 DD:HH:MM:SS 格式来使其更清晰。

Example:

例子:

10.89 hours == 10 hours, 53 minutes, 40 seconds

10.89 小时 == 10 小时 53 分 40 秒

EDIT: 10.894945454545455 == 10 hours, 53 minutes, 40 seconds

编辑:10.894945454545455 == 10 小时 53 分 40 秒

What I've tried:

我试过的:

int hours = (int) ((finalBuildTime) % 1);
int minutes = (int) ((finalBuildTime * (60*60)) % 60);
int seconds = (int) ((finalBuildTime * 3600) % 60);

return String.format("%s(h) %s(m) %s(s)", hours, minutes, seconds);

Which returned: 0(h) 41(m) 41(s)

哪个返回: 0(h) 41(m) 41(s)

Any suggestions?

有什么建议?

采纳答案by JustinKSU

  1. There is no need to do a modular on minutes.
  2. Your calculation of minutes should just multiply by 60, not (60*60)

    double finalBuildTime = 10.89;
    int hours = (int) finalBuildTime;
    int minutes = (int) (finalBuildTime * 60) % 60;
    int seconds = (int) (finalBuildTime * (60*60)) % 60;
    
    System.out.println(String.format("%s(h) %s(m) %s(s)", hours, minutes, seconds));
    
  1. 无需在几分钟内进行模块化。
  2. 您计算的分钟数应该乘以 60,而不是 (60*60)

    double finalBuildTime = 10.89;
    int hours = (int) finalBuildTime;
    int minutes = (int) (finalBuildTime * 60) % 60;
    int seconds = (int) (finalBuildTime * (60*60)) % 60;
    
    System.out.println(String.format("%s(h) %s(m) %s(s)", hours, minutes, seconds));
    

This code gives you the correct output

此代码为您提供正确的输出

10(h) 53(m) 24(s)

I believe your expected output of 40 seconds is incorrect. It should be 24 seconds.

我相信您预期的 40 秒输出是不正确的。应该是 24 秒。

(53*60 + 24)/(60*60) = 0.89

回答by Jobs

  1. (int) rounds down. Therefore, (int)(finalBuildTime) gives you hours.

  2. Since 60 times 60 is 3600, your seconds and minutes calculations are the same.

  3. To find the minutes and seconds, I would observe that the 10.89 hours in your example is 10 hours and 0.89 hours. I would first calculate how many minutes is in 0.89 hours.(the part after the decimal point) That would be 0.89 times 60. Again, since this number should always be less than 60, casting(therefore, rounding down) the number to an (int) gives you minutes. Therefore, for minutes, it is int minutes = (int)((finalBuildTime-hours)*60);

  4. You can keep the rest of your code.

  1. (int) 向下舍入。因此, (int)(finalBuildTime) 为您提供数小时。

  2. 由于 60 乘以 60 是 3600,因此您的秒和分钟计算是相同的。

  3. 要找到分钟和秒,我会观察到您示例中的 10.89 小时是 10 小时和 0.89 小时。我将首先计算 0.89 小时内有多少分钟。(小数点后的部分)那将是 0.89 乘以 60。同样,由于这个数字应该始终小于 60,因此将数字(因此,四舍五入)转换为(int) 给你分钟。因此,对于分钟,它是 int minutes = (int)((finalBuildTime-hours)*60);

  4. 您可以保留其余代码。

回答by epichorns

Here is a complete implementation:

这是一个完整的实现:

package test.std.java;

public class Test {

    public static void main(String[] args) {
        System.out.println(GetReadableTime(10.89));
    }

    //Prints outs HH:MM:SS
    public static String GetReadableTime(double finalBuildTime){

        int hours = (int) Math.floor(finalBuildTime);
        int remainderInSeconds = (int)(3600.0* (finalBuildTime - Math.floor(finalBuildTime)) );
        int seconds = remainderInSeconds % 60;
        int minutes = remainderInSeconds / 60;
        return  String.format("%02d:%02d:%02d", hours, minutes, seconds); 
    }
}

回答by Serge Ballesta

First part of rgettman is true :

rgettman 的第一部分是真的:

float finalBuildTime = 10.89;
int hours = (int) finalBuildTime; // 10 ok

But (int) ((10.89 * (60 *60)) / 60) = 683 which is not what you want : it is the direct conversion of finalBuildTimein minutes

但是 (int) ((10.89 * (60 *60)) / 60) = 683 这不是你想要的:它是以finalBuildTime分钟为单位的直接转换

int minutes = (int) ((finalBuildTime - hours) * 60); // 53 ok
int seconds = (int) ((finalBuildTime - hours) * 3600 - minutes * 60 + 0.5); // 24 ok

I've added 0.5 for the seconds computation to round to the nearest second rather than truncate. For your example it is no use because your time is an integer number of seconds.

我为秒计算添加了 0.5 以舍入到最接近的秒而不是截断。对于您的示例,它没有用,因为您的时间是整数秒。

And the number of seconds is24 seconds = 0.4 minutes

秒数24 seconds = 0.4 minutes

回答by Christian Wilkie

The other answers are correct, but in case it helps you (or someone else who stumbles across this) it is a lot easier to use a library like Joda-Timeto do this. Here is a solution to your problem using Joda-Time 2.3:

其他答案是正确的,但如果它对你(或其他偶然发现的人)有帮助,使用像Joda-Time这样的库来做到这一点要容易得多。这是使用 Joda-Time 2.3 解决您的问题的方法:

double buildDurationInHours = 10.89;
long buildDurationInMilliseconds = new Double(DateTimeConstants.MILLIS_PER_HOUR * buildDurationInHours).longValue();
Period period = new Period(buildDurationInMilliseconds);
System.out.println("The build took "
        + period.getDays() + " days, "
        + period.getHours() + " hours, "
        + period.getMinutes() + " minutes, and "
        + period.getSeconds() + " seconds.");

Output:

输出:

The build took 0 days, 10 hours, 53 minutes, and 24 seconds.

This way you don't have to deal directly with calculating the hours, minutes, seconds, etc.

这样你就不必直接处理计算小时、分钟、秒等。

回答by Christian Wilkie

I assume finalBuildTime is a double. You just have to do :

我假设 finalBuildTime 是双倍的。你只需要做:

int hours = (int)finalBuildTime;
int minutes = (int)((finalBuildTime - hours) * 60);
int seconds = (int)((((finalBuildTime - hours) * 60) - minutes ) * 60);