将毫秒 换算成(天 时 分 秒 毫秒)
程序员文章站
2022-04-30 11:29:09
...
- /*
- * 毫秒转化
- */
- public static String formatTime(long ms) {
- int ss = 1000;
- int mi = ss * 60;
- int hh = mi * 60;
- int dd = hh * 24;
- long day = ms / dd;
- long hour = (ms - day * dd) / hh;
- long minute = (ms - day * dd - hour * hh) / mi;
- long second = (ms - day * dd - hour * hh - minute * mi) / ss;
- long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;
- String strDay = day < 10 ? "0" + day : "" + day; //天
- String strHour = hour < 10 ? "0" + hour : "" + hour;//小时
- String strMinute = minute < 10 ? "0" + minute : "" + minute;//分钟
- String strSecond = second < 10 ? "0" + second : "" + second;//秒
- String strMilliSecond = milliSecond < 10 ? "0" + milliSecond : "" + milliSecond;//毫秒
- strMilliSecond = milliSecond < 100 ? "0" + strMilliSecond : "" + strMilliSecond;
- return strMinute + " 分钟 " + strSecond + " 秒";
- }
下面是一样的,输出格式不同而已
- /*
- * 毫秒转化时分秒毫秒
- */
- public static String formatTime(Long ms) {
- Integer ss = 1000;
- Integer mi = ss * 60;
- Integer hh = mi * 60;
- Integer dd = hh * 24;
- Long day = ms / dd;
- Long hour = (ms - day * dd) / hh;
- Long minute = (ms - day * dd - hour * hh) / mi;
- Long second = (ms - day * dd - hour * hh - minute * mi) / ss;
- Long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;
- StringBuffer sb = new StringBuffer();
- if(day > 0) {
- sb.append(day+"天");
- }
- if(hour > 0) {
- sb.append(hour+"小时");
- }
- if(minute > 0) {
- sb.append(minute+"分");
- }
- if(second > 0) {
- sb.append(second+"秒");
- }
- if(milliSecond > 0) {
- sb.append(milliSecond+"毫秒");
- }
- return sb.toString();
- }