Java tutorial
//package com.java2s; //License from project: Apache License import java.util.Locale; public class Main { static final int MISEC_UINTS_SECOND = 1000; static final int SECOND_UINTS_MINUTE = 60; static final int MINUTE_UNINTS_HOUR = 60; static final int SECOND_UINTS_HOUR = 60 * 60; static final int SECOND_UINTS_DAY = 24 * 60 * 60; public static String formatDuration(long duration) { String strTime = "00:00"; do { if (duration <= 0) { break; } long second = duration / MISEC_UINTS_SECOND; if (second < SECOND_UINTS_MINUTE) { strTime = String.format(Locale.ENGLISH, "00:%02d", second); break; } if (second < SECOND_UINTS_HOUR) { strTime = String.format(Locale.ENGLISH, "%02d:%02d", second / MINUTE_UNINTS_HOUR, second % SECOND_UINTS_MINUTE); break; } if (second < SECOND_UINTS_DAY) { strTime = String.format(Locale.ENGLISH, "%02d:%02d:%02d", second / SECOND_UINTS_HOUR, (second % (SECOND_UINTS_HOUR)) / MINUTE_UNINTS_HOUR, (second % (SECOND_UINTS_HOUR)) % SECOND_UINTS_MINUTE); break; } if (second > SECOND_UINTS_DAY) { strTime = second + ""; break; } } while (false); return strTime; } }