package com.visy.utils;
public class TimeUtils {
private static String millisToStr0(long millis){
long totalSeconds = millis/1000;
if(totalSeconds < 60){
return totalSeconds+"秒";
}else if(totalSeconds < 3600){
long m = totalSeconds/60, s = totalSeconds%60;
return m + "分" + s + "秒";
}else{
long h = totalSeconds/3600;
totalSeconds = totalSeconds%3600;
long m = totalSeconds/60, s = totalSeconds%60;
return h + "小时" + m + "分" + s + "秒";
}
}
private static String millisToStr(long millis, String ...units){
int size = units.length;
if(size == 0){
return millis+"ms";
}
long seconds = millis/1000;
int index = 1;
StringBuilder spendTime = new StringBuilder();
for(String unit: units){
int base = (int) Math.pow(60, size-index);
if(seconds < base){
continue;
}
spendTime.append(seconds/base).append(unit);
seconds = seconds % base;
index ++;
}
return spendTime.toString();
}
public static void main(String[] args) {
System.out.println(millisToStr0(35793000L));
System.out.println("-----------------------------");
System.out.println(millisToStr(1000));
System.out.println(millisToStr(1000, "秒"));
System.out.println(millisToStr(35793000L, "秒"));
System.out.println(millisToStr(35793000L, "分","秒"));
System.out.println(millisToStr(35793000L, "小时","分","秒"));
}
}
9小时56分33秒
-----------------------------
1000ms
1秒
35793秒
596分33秒
9小时56分33秒