使用旧的 java 日期 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);
也可以看看SimpleDateFormat http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html.
注意:根据评论,如果秒数超过一天中的秒数 (86400),则将无法按预期工作。在这种情况下,必须采取不同的方法。
EDIT: 如果你正在使用JDK 8 http://docs.oracle.com/javase/8/docs/api/java/time/Duration.html你可以写:
int sec = ...
Duration duration = Duration.ofSeconds(sec);
这个持续时间对象更好地代表了您正在谈论的内容。我正在尝试找出如何按照您想要的方式格式化它,但到目前为止我还没有运气。
EDIT 2:在 JDK 8 之前,您可以使用Joda API http://joda-time.sourceforge.net/apidocs/org/joda/time/Period.html:
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 https://stackoverflow.com/questions/1440557/joda-time-period-to-string.
EDIT 3:根据评论,我明确添加了时区。