如何将秒数转换为累计 hhh:mm:ss?
How do I convert seconds into accumulated hhh:mm:ss?
我想将经过的累计秒数转换为 hours:minutes:seconds。例如
93599 秒到:
25:59:59
我该怎么做? (注意:24h 不应换行为 0h)
复制并使用此 Class:
public class AccumulatedTimeFormat extends Format {
@Override
public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
Long totalSeconds;
if(obj instanceof Byte) {
totalSeconds=((Byte)obj).longValue();
} else if(obj instanceof Short) {
totalSeconds=((Short)obj).longValue();
} else if(obj instanceof Integer) {
totalSeconds=((Integer)obj).longValue();
} else if(obj instanceof Long) {
totalSeconds=((Long)obj);
} else {
throw new IllegalArgumentException("Cannot format given Object as an accumulated-time String!");
}
long ss = Math.abs(totalSeconds) % 60;
long mm = (Math.abs(totalSeconds) / 60) % 60;
long h = (Math.abs(totalSeconds) / 60) / 60;
if(totalSeconds<0) {
toAppendTo.append('-');
}
toAppendTo.append(String.format("%d:%02d:%02d", h, mm, ss));
return toAppendTo;
}
@Override
public Object parseObject(String source, ParsePosition pos) {
//TODO Implement if needed!
return null;
}
}
像这样:
System.out.println((new AccumulatedTimeFormat()).format(93599));
打印:
25:59:59
小时 = 93599 / 3600。使用整数算法强制截断。
然后从 93599 中减去小时数 * 3600。将其命名为 foo。或者计算 93599 % 3600.
分钟 = foo / 60。使用整数运算。
从 foo 中减去分钟 * 60。打电话给那个酒吧。或者计算 foo % 60.
bar 是剩余秒数。
Java 8 提供 java.time.Duration
class for expressing amounts of time. You can use Duration#ofSeconds(long)
从秒数构建实例。
Duration ellapsed = Duration.ofSeconds(93599);
但是,默认的 toString
格式看起来像
PT25H59M59S
这不是您想要的。您可以自己计算(使用各种 toMinutes
、toHours
等)将其转换为您想要的格式。举个例子here.
模运算符%
在这里很有用。这样的东西会很好用
public static void convertTime(int seconds) {
int secs = seconds % 60;
int mins = (seconds / 60) % 60;
int hours = (seconds / 60) / 60;
System.out.printf("%d:%d:%d", hours, mins, secs);
}
我想将经过的累计秒数转换为 hours:minutes:seconds。例如
93599 秒到:
25:59:59
我该怎么做? (注意:24h 不应换行为 0h)
复制并使用此 Class:
public class AccumulatedTimeFormat extends Format {
@Override
public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
Long totalSeconds;
if(obj instanceof Byte) {
totalSeconds=((Byte)obj).longValue();
} else if(obj instanceof Short) {
totalSeconds=((Short)obj).longValue();
} else if(obj instanceof Integer) {
totalSeconds=((Integer)obj).longValue();
} else if(obj instanceof Long) {
totalSeconds=((Long)obj);
} else {
throw new IllegalArgumentException("Cannot format given Object as an accumulated-time String!");
}
long ss = Math.abs(totalSeconds) % 60;
long mm = (Math.abs(totalSeconds) / 60) % 60;
long h = (Math.abs(totalSeconds) / 60) / 60;
if(totalSeconds<0) {
toAppendTo.append('-');
}
toAppendTo.append(String.format("%d:%02d:%02d", h, mm, ss));
return toAppendTo;
}
@Override
public Object parseObject(String source, ParsePosition pos) {
//TODO Implement if needed!
return null;
}
}
像这样:
System.out.println((new AccumulatedTimeFormat()).format(93599));
打印:
25:59:59
小时 = 93599 / 3600。使用整数算法强制截断。
然后从 93599 中减去小时数 * 3600。将其命名为 foo。或者计算 93599 % 3600.
分钟 = foo / 60。使用整数运算。
从 foo 中减去分钟 * 60。打电话给那个酒吧。或者计算 foo % 60.
bar 是剩余秒数。
Java 8 提供 java.time.Duration
class for expressing amounts of time. You can use Duration#ofSeconds(long)
从秒数构建实例。
Duration ellapsed = Duration.ofSeconds(93599);
但是,默认的 toString
格式看起来像
PT25H59M59S
这不是您想要的。您可以自己计算(使用各种 toMinutes
、toHours
等)将其转换为您想要的格式。举个例子here.
模运算符%
在这里很有用。这样的东西会很好用
public static void convertTime(int seconds) {
int secs = seconds % 60;
int mins = (seconds / 60) % 60;
int hours = (seconds / 60) / 60;
System.out.printf("%d:%d:%d", hours, mins, secs);
}