Here you can find the source of formatTime(long timeInMilli)
Parameter | Description |
---|---|
timeInMilli | time in milli-seconds |
String
representing the formatted time...
public static String formatTime(long timeInMilli)
//package com.java2s; public class Main { /**// w w w . j av a 2 s . c o m * Formats a time given in millisecond. The output will be "X hours Y min Z sec", unless X, Y or Z is 0 in which * case that part of the string will be omitted. * <p/> * Created on May 3, 2004 by Fabian Depry * @param timeInMilli time in milli-seconds * @return a <code>String</code> representing the formatted time... */ public static String formatTime(long timeInMilli) { long hourBasis = 60; StringBuilder formattedTime = new StringBuilder(); long secTmp = timeInMilli / 1000; long sec = secTmp % hourBasis; long minTmp = secTmp / hourBasis; long min = minTmp % hourBasis; long hour = minTmp / hourBasis; if (hour > 0) { formattedTime.append(hour).append(" hour"); if (hour > 1) formattedTime.append("s"); } if (min > 0) { if (formattedTime.length() > 0) formattedTime.append(", "); formattedTime.append(min).append(" minute"); if (min > 1) formattedTime.append("s"); } if (sec > 0) { if (formattedTime.length() > 0) formattedTime.append(", "); formattedTime.append(sec).append(" second"); if (sec > 1) formattedTime.append("s"); } if (formattedTime.length() > 0) return formattedTime.toString(); return "< 1 second"; } }