格式化Java字符串时间格式化
有没有办法不用if ( hours > 0 )
就可以做到这一点?我觉得必须有一种方法来指示数字的条件显示,但我在javadocs或google中找不到它
public String getLengthDisplay () {
int hours = getLength() / 3600;
int minutes = ( getLength() % 3600 ) / 60;
int seconds = getLength() % 60;
if ( hours > 0 ) {
return String.format ( "%d:%02d:%02d", hours, minutes, seconds );
} else {
return String.format ( "%d:%02d", minutes, seconds );
}
}
谢谢
# 1 楼答案
无法使用
format()
,只需修剪前导零:如果小时和分钟均为零,则此代码还会对分钟进行微调。如果你总是想要会议记录,请从这段代码中删除
(00:)?
# 2 楼答案
我认为没有时间,代码不会灵活>;0条件。 修剪也是一个不错的选择
检查上面的代码,返回小时-分钟-秒格式,如果需要,也可以添加分钟条件