Java 如何将秒转换为 hhmmss
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22545644/
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
how to convert seconds into hhmmss
提问by user3397972
i have looked everywhere to convert seconds into hh:mm:ss but couldn't find the right one
我到处寻找将秒转换为 hh:mm:ss 但找不到合适的
i created a program that allows a user to enter two different times and then calculate the difference
我创建了一个程序,允许用户输入两个不同的时间,然后计算差异
the times entered are split in hh * 3600 - mm * 60 - ss then converted into seconds and subtracted from each other to calculate difference in seconds
输入的时间分为 hh * 3600 - mm * 60 - ss 然后转换为秒并相互减去以计算秒差
for example 12:12:12 and 13:13:13 would give me 3661 seconds but i don't know how to convert the difference back into hh:mm:ss
例如 12:12:12 和 13:13:13 会给我 3661 秒,但我不知道如何将差异转换回 hh:mm:ss
any help would be appreciated
任何帮助,将不胜感激
采纳答案by Giovanni Botta
Using the old java date api (not recommended, see comments):
使用旧的 java date api(不推荐,见评论):
int sec = .... //
Date d = new Date(sec * 1000L);
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss"); // HH for 0-23
df.setTimeZone(TimeZone.getTimeZone("GMT"));
String time = df.format(d);
See also SimpleDateFormat
.
Note: as per comments, if the number of seconds exceeds the number of seconds in a day (86400) that won't work as expected. In that case a different approach must be taken.
注意:根据评论,如果秒数超过一天中的秒数(86400),则无法按预期工作。在这种情况下,必须采取不同的方法。
EDIT: If you're using JDK 8you can write:
编辑:如果您使用的是JDK 8,您可以编写:
int sec = ...
Duration duration = Duration.ofSeconds(sec);
This duration object better represents what you're talking about. I am trying to find out how to format it as you want to but I have had no luck so far.
这个持续时间对象更好地代表了你在说什么。我试图找出如何按照你的意愿格式化它,但到目前为止我没有运气。
EDIT 2: prior to JDK 8 you can use the Joda API:
编辑 2:在 JDK 8 之前,您可以使用Joda API:
int sec = ...
Period period = new Period(sec * 1000L);
String time = PeriodFormat.getDefault().print(period); // you can customize the format if this one doesn't cut it
That's probably the most elegant solution. See also this.
这可能是最优雅的解决方案。另请参见本。
EDIT 3: As per comments, I explicitly added the time zone.
编辑 3:根据评论,我明确添加了时区。
回答by JustSoAmazing
Just in case you're looking to write your own algorithm for this:
以防万一您想为此编写自己的算法:
Let's say we have 1000 seconds.
假设我们有 1000 秒。
We know that there're 3600 seconds in an hour, so when we format this time as hh:mm:ss, the hh field will be 00. Now let's say we're given a time of 3700 seconds. This time interval is slightly larger than 1 hour, so we know that the hh field will show 01.
我们知道一小时有 3600 秒,所以当我们将这个时间格式化为 hh:mm:ss 时,hh 字段将为 00。现在假设我们得到了 3700 秒的时间。这个时间间隔略大于1小时,所以我们知道hh字段会显示01。
So to calculate the number for the hh field, simply divide the provided seconds amount by 3600.
因此,要计算 hh 字段的数字,只需将提供的秒数除以 3600。
int hours = seconds / 3600
int hours = seconds / 3600
Note that when we have a seconds amount greater than 3600, the result is truncated, so we're left with an integer amount for the hours.
请注意,当我们的秒数大于 3600 时,结果会被截断,因此我们只剩下一个整数作为小时数。
Moving on to the mm field. Again, let's assume we're given a time interval of 3700 seconds. We already know that 3700 seconds is slightly more than 1 hour - we've stored the number of hours in the hour
field. To calculate the number of minutes, we'll subtract the hours times 3600 from the provided seconds input:
移至 mm 字段。同样,让我们假设给定的时间间隔为 3700 秒。我们已经知道 3700 秒略大于 1 小时 - 我们已经在hour
现场存储了小时数。为了计算分钟数,我们将从提供的秒输入中减去小时乘以 3600:
int minutes = (seconds - hours * 3600) / 60
int minutes = (seconds - hours * 3600) / 60
So if we have a provided time of 3700 seconds, the above code translates to (3700 - 3600) / 60 - we divide by 60 because we want to convert from seconds to minutes.
所以如果我们提供 3700 秒的时间,上面的代码转换为 (3700 - 3600) / 60 - 我们除以 60,因为我们想从秒转换为分钟。
Finally, the ss field. We use a similar technique as above to calculate the number of seconds.
最后是ss字段。我们使用与上述类似的技术来计算秒数。
int seconds = (seconds - hours * 3600) - minutes * 60
int seconds = (seconds - hours * 3600) - minutes * 60
public static String formatSeconds(int timeInSeconds)
{
int hours = timeInSeconds / 3600;
int secondsLeft = timeInSeconds - hours * 3600;
int minutes = secondsLeft / 60;
int seconds = secondsLeft - minutes * 60;
String formattedTime = "";
if (hours < 10)
formattedTime += "0";
formattedTime += hours + ":";
if (minutes < 10)
formattedTime += "0";
formattedTime += minutes + ":";
if (seconds < 10)
formattedTime += "0";
formattedTime += seconds ;
return formattedTime;
}
回答by isak gilbert
Use JODA time libary http://joda-time.sourceforge.net/apidocs/index.html
使用 JODA 时间库http://joda-time.sourceforge.net/apidocs/index.html
PeriodFormatter formatter = ... // Define your formatting here
Period firstPeriod = new Period(13, 13, 13, 0);
Period secondPeriod = new Period(12, 12, 12, 0);
Period resultPeriod = firstPeriod.minus(secondPeriod);
String formattedPeriod = formatter.print(resultPeriod);
// Display period e.g. System.out.println(formatterPeriod);
// ....
回答by Sebastian Londono
This is a shorter method:
这是一个较短的方法:
public static String formatSeconds(int timeInSeconds)
{
int secondsLeft = timeInSeconds % 3600 % 60;
int minutes = (int) Math.floor(timeInSeconds % 3600 / 60);
int hours = (int) Math.floor(timeInSeconds / 3600);
String HH = ((hours < 10) ? "0" : "") + hours;
String MM = ((minutes < 10) ? "0" : "") + minutes;
String SS = ((secondsLeft < 10) ? "0" : "") + secondsLeft;
return HH + ":" + MM + ":" + SS;
}
回答by BarBar1234
public static void formatSeconds(Integer result){
System.out.print(String.format("%02d",result/3600)+":");
System.out.print(String.format("%02d",result/60%60)+":");
System.out.println(String.format("%02d",result%60));
}