Here you can find the source of formatTimePeriod(long millis)
public static String formatTimePeriod(long millis)
//package com.java2s; //License from project: Apache License public class Main { public static String formatTimePeriod(long millis) { long seconds = (millis / 1000) % 60; long minutes = (millis / (1000 * 60)) % 60; long hours = (millis / (1000 * 60 * 60));// %24; long onlyMillis = millis % 1000; boolean hasMillis = onlyMillis > 0; if (hours > 0) { String format = "%d:%02d:%02d"; if (hasMillis) { format += ".%03d"; } else { format += ".0"; }//from w w w . j av a 2 s . co m return String.format(format, hours, minutes, seconds, onlyMillis); } else if (minutes > 0) { String format = "%d:%02d"; if (hasMillis) { format += ".%03d"; } else { format += ".0"; } return String.format(format, minutes, seconds, onlyMillis); } else if (seconds > 0) { String format = "%d"; if (hasMillis) { format += ".%03d seconds"; } else { format += ".0 seconds"; } return String.format(format, seconds, onlyMillis); } else { return String.format("%d milliseconds", onlyMillis); } } }