项目中突然要用到将秒数转为*天*时*分的方法,但是在网上找了几遍发现没有想要的效果,于是自己写了一个。代码如下
public class DateUtil {
/**
* 将秒数转化为*天*时*秒
* @param seconds
* @return
*/
public static String secondConvert(long seconds){
String html = "0分";
if (seconds != 0) {
String format = "";
Object[] array;
int days = (int)(seconds / (60*60*24));
int hours = (int)(seconds/3600 - days*24);
int minutes = (int)(seconds/60 - hours*60 - days*24*60);
if (days > 0) {
format = "%1$,d天%2$,d时%3$,d分";
array = new Object[]{days,hours,minutes};
}else if(hours > 0){
format = "%1$,d时%2$,d分";
array = new Object[]{hours,minutes};
}else {
format = "%1$,d分";
array = new Object[]{minutes};
}
html = String.format(format, array);
}
return html;
}
}
运行结果:
30000s:8时20分