【发布时间】:2014-03-20 21:35:56
【问题描述】:
我到处寻找将秒转换为 hh:mm:ss 的方法,但找不到合适的
我创建了一个程序,允许用户输入两个不同的时间,然后计算差异
输入的时间以 hh * 3600 - mm * 60 - ss 为单位,然后转换为秒并相互减去以计算以秒为单位的差异
例如 12:12:12 和 13:13:13 会给我 3661 秒,但我不知道如何将差异转换回 hh:mm:ss
任何帮助将不胜感激
【问题讨论】:
标签: java
我到处寻找将秒转换为 hh:mm:ss 的方法,但找不到合适的
我创建了一个程序,允许用户输入两个不同的时间,然后计算差异
输入的时间以 hh * 3600 - mm * 60 - ss 为单位,然后转换为秒并相互减去以计算以秒为单位的差异
例如 12:12:12 和 13:13:13 会给我 3661 秒,但我不知道如何将差异转换回 hh:mm:ss
任何帮助将不胜感激
【问题讨论】:
标签: java
使用旧的 java date api(不推荐,参见 cmets):
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);
注意:根据 cmets,如果秒数超过一天中的秒数 (86400),则无法按预期工作。在这种情况下,必须采取不同的方法。
编辑:如果你使用JDK 8,你可以写:
int sec = ...
Duration duration = Duration.ofSeconds(sec);
这个持续时间对象更好地代表了您正在谈论的内容。我正在尝试找出如何按照您的意愿格式化它,但到目前为止我还没有运气。
编辑 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
这可能是最优雅的解决方案。另见this。
编辑 3:根据 cmets,我明确添加了时区。
【讨论】:
CET – 中欧时间GMT +1 hour 这将为sec = 0 提供01:00:00。在这里,纯匹配似乎更容易。
以防万一您想为此编写自己的算法:
假设我们有 1000 秒。
我们知道一小时有 3600 秒,所以当我们将此时间格式化为 hh:mm:ss 时,hh 字段将为 00。现在假设我们的时间为 3700 秒。这个时间间隔略大于1小时,所以我们知道hh字段会显示01。
因此,要计算 hh 字段的数字,只需将提供的秒数除以 3600。
int hours = seconds / 3600
请注意,当我们的秒数大于 3600 时,结果会被截断,因此我们会为小时留下一个整数。
转到 mm 字段。同样,假设我们的时间间隔为 3700 秒。我们已经知道 3700 秒略大于 1 小时 - 我们已将小时数存储在 hour 字段中。要计算分钟数,我们将从提供的秒输入中减去小时乘以 3600:
int minutes = (seconds - hours * 3600) / 60
因此,如果我们提供了 3700 秒的时间,上述代码将转换为 (3700 - 3600) / 60 - 我们除以 60,因为我们想将秒转换为分钟。
最后是 ss 字段。我们使用与上述类似的技术来计算秒数。
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;
}
【讨论】:
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));
}
【讨论】:
这是一个较短的方法:
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;
}
【讨论】:
我知道现在回答这个问题有点晚了,但没关系。 它与 BarBar1234 的答案基本相同,但您可以将它们全部组合成一个字符串:
String.format("%02d:%02d:%02d", seconds / 3600, (seconds / 60) % 60, seconds % 60);
【讨论】:
使用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);
// ....
【讨论】: