如何将毫秒转换为“hh:mm:ss”格式?

我很困惑。 在绊倒这个线程之后,我试图弄清楚如何格式化hh:mm:ss格式的倒数计时器。

这是我的尝试 –

 //hh:mm:ss String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis), TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.MINUTES.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)), TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))); 

所以,当我尝试一个像3600000ms的值,我得到01:59:00 ,这是错误的,因为它应该是01:00:00 。 很明显,我的逻辑有些问题,但是现在我看不到它是什么了!

谁能帮忙?

编辑 –

修复。 下面是将毫秒格式化为hh:mm:ss格式的正确方法 –

 //hh:mm:ss String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis), TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)), TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)))); 

问题是这个TimeUnit.MINUTES.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)) 。 应该是这个TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis))

真的很接近:

 String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis), TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)), // The change is in this line TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))); 

你使用几分钟而不是几个小时将小时转换为毫秒。

顺便说一句,我喜欢你使用TimeUnit API 🙂

以下是一些testing代码:

 public static void main(String[] args) throws ParseException { long millis = 3600000; String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis), TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)), TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))); System.out.println(hms); } 

输出:

 01:00:00 

我意识到我上面的代码可以通过使用模而不是减法来简化:

 String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis), TimeUnit.MILLISECONDS.toMinutes(millis) % TimeUnit.HOURS.toMinutes(1), TimeUnit.MILLISECONDS.toSeconds(millis) % TimeUnit.MINUTES.toSeconds(1)); 

仍然使用TimeUnit API处理所有魔术值,并提供完全相同的输出。

通用的方法是相当简单的:

 public static String convertSecondsToHMmSs(long seconds) { long s = seconds % 60; long m = (seconds / 60) % 60; long h = (seconds / (60 * 60)) % 24; return String.format("%d:%02d:%02d", h,m,s); } 

如果你使用的是Apache Commons:

 DurationFormatUtils.formatDuration(timeInMS, "HH:mm:ss,SSS"); 
 // New date object from millis Date date = new Date(millis); // formattter SimpleDateFormat formatter= new SimpleDateFormat("HH:mm:ss"); // Pass date object String formatted = formatter.format(date ); 
 DateFormat df = new SimpleDateFormat("HH:mm:ss"); String formatted = df.format(aDateObject); 

我用这个:

 String.format("%1$tH:%1$tM:%1$tS", millis); 

资料来源: http : //docs.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html

4个实现的testing结果

不得不对大量数据进行大量格式化,需要最好的性能,所以下面是(令人惊讶的)结果:

for(int i = 0; i <1000000; i ++){FUNCTION_CALL}

持续时间:

  • 组合forms: 196毫米
  • formatDuration: 272毫秒
  • apacheFormat:754毫秒
  • formatTimeUnit:2216毫秒

     public static String apacheFormat(long millis) throws ParseException { return DurationFormatUtils.formatDuration(millis, "HH:mm:ss"); } public static String formatTimeUnit(long millis) throws ParseException { String formatted = String.format( "%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis), TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)), TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))); return formatted; } public static String formatDuration(final long millis) { long seconds = (millis / 1000) % 60; long minutes = (millis / (1000 * 60)) % 60; long hours = millis / (1000 * 60 * 60); StringBuilder b = new StringBuilder(); b.append(hours == 0 ? "00" : hours < 10 ? String.valueOf("0" + hours) : String.valueOf(hours)); b.append(":"); b.append(minutes == 0 ? "00" : minutes < 10 ? String.valueOf("0" + minutes) : String.valueOf(minutes)); b.append(":"); b.append(seconds == 0 ? "00" : seconds < 10 ? String.valueOf("0" + seconds) : String.valueOf(seconds)); return b.toString(); } public static String combinationFormatter(final long millis) { long seconds = TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)); long minutes = TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)); long hours = TimeUnit.MILLISECONDS.toHours(millis); StringBuilder b = new StringBuilder(); b.append(hours == 0 ? "00" : hours < 10 ? String.valueOf("0" + hours) : String.valueOf(hours)); b.append(":"); b.append(minutes == 0 ? "00" : minutes < 10 ? String.valueOf("0" + minutes) : String.valueOf(minutes)); b.append(":"); b.append(seconds == 0 ? "00" : seconds < 10 ? String.valueOf("0" + seconds) : String.valueOf(seconds)); return b.toString(); } 

下面的代码以两种方式进行转换

23:59:58:999到86398999

然后

86398999至23:59:58:999


 import java.util.concurrent.TimeUnit; public class TimeUtility { public static void main(String[] args) { long currentDateTime = System.currentTimeMillis(); String strTest = "23:59:58:999"; System.out.println(strTest); long l = strToMilli(strTest); System.out.println(l); l += 1; String str = milliToString(l); System.out.println(str); } /** * convert a time string into the equivalent long milliseconds * * @param strTime string fomratted as HH:MM:SS:MSMS ie "23:59:59:999" * @return long integer like 86399999 */ public static long strToMilli(String strTime) { long retVal = 0; String hour = strTime.substring(0, 2); String min = strTime.substring(3, 5); String sec = strTime.substring(6, 8); String milli = strTime.substring(9, 12); int h = Integer.parseInt(hour); int m = Integer.parseInt(min); int s = Integer.parseInt(sec); int ms = Integer.parseInt(milli); String strDebug = String.format("%02d:%02d:%02d:%03d", h, m, s, ms); //System.out.println(strDebug); long lH = h * 60 * 60 * 1000; long lM = m * 60 * 1000; long lS = s * 1000; retVal = lH + lM + lS + ms; return retVal; } /** * convert time in milliseconds to the corresponding string, in case of day * rollover start from scratch 23:59:59:999 + 1 = 00:00:00:000 * * @param millis the number of milliseconds corresponding to tim ie * 34137999 that can be obtained as follows; * <p> * long lH = h * 60 * 60 * 1000; //hour to milli * <p> * long lM = m * 60 * 1000; // minute to milli * <p> * long lS = s * 1000; //seconds to milli * <p> * millis = lH + lM + lS + ms; * @return a string formatted as HH:MM:SS:MSMS ie "23:59:59:999" */ private static String milliToString(long millis) { long hrs = TimeUnit.MILLISECONDS.toHours(millis) % 24; long min = TimeUnit.MILLISECONDS.toMinutes(millis) % 60; long sec = TimeUnit.MILLISECONDS.toSeconds(millis) % 60; //millis = millis - (hrs * 60 * 60 * 1000); //alternative way //millis = millis - (min * 60 * 1000); //millis = millis - (sec * 1000); //long mls = millis ; long mls = millis % 1000; String toRet = String.format("%02d:%02d:%02d:%03d", hrs, min, sec, mls); //System.out.println(toRet); return toRet; } } 

通过波希米亚的答案,我们不需要使用TimeUnit来find一个已知的值。 更多的最佳代码将是

 String hms = String.format("%02d:%02d:%02d", millisLeft/(3600*1000), millisLeft/(60*1000) % 60, millisLeft/1000 % 60); 

希望它有帮助

  public String millsToDateFormat(long mills) { Date date = new Date(mills); DateFormat formatter = new SimpleDateFormat("HH:mm:ss"); String dateFormatted = formatter.format(date); return dateFormatted; //note that it will give you the time in GMT+0 } 

我试着如第一个答案所示。 它工作,但减去了我的困惑。 Groovy的回答:

 import static java.util.concurrent.TimeUnit.* ... private static String formatElapsedTime(long millis) { int hrs = MILLISECONDS.toHours(millis) % 24 int min = MILLISECONDS.toMinutes(millis) % 60 int sec = MILLISECONDS.toSeconds(millis) % 60 int mls = millis % 1000 sprintf( '%02d:%02d:%02d (%03d)', [hrs, min, sec, mls]) } 
  String string = String.format("%02d:%02d:%02d.%03d", TimeUnit.MILLISECONDS.toHours(millisecend), TimeUnit.MILLISECONDS.toMinutes(millisecend) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millisecend)), TimeUnit.MILLISECONDS.toSeconds(millisecend) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisecend)), millisecend - TimeUnit.SECONDS.toMillis(TimeUnit.MILLISECONDS.toSeconds(millisecend))); 

格式:00:00:00.000

例如:615605 Millisecend

00:10:15.605

那么,你可以尝试这样的事情:

 public String getElapsedTimeHoursMinutesSecondsString() { long elapsedTime = getElapsedTime(); String format = String.format("%%0%dd", 2); elapsedTime = elapsedTime / 1000; String seconds = String.format(format, elapsedTime % 60); String minutes = String.format(format, (elapsedTime % 3600) / 60); String hours = String.format(format, elapsedTime / 3600); String time = hours + ":" + minutes + ":" + seconds; return time; } 

将毫秒转换为时间值

标记为正确的答案有一点小错,

 String myTime = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis), TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)), // The change is in this line TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))); 

例如,这是我得到的价值的一个例子:

 417474:44:19 

这是获得正确格式的解决scheme是:

 String myTime = String.format("%02d:%02d:%02d", //Hours TimeUnit.MILLISECONDS.toHours(millis) - TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(millis)), //Minutes TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)), //Seconds TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))); 

得到一个正确的格式:

 18:44:19 

其他选项来获得格式hh:mm:ss只是:

  Date myDate = new Date(timeinMillis); SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss"); String myTime = formatter.format(myDate); 
Interesting Posts