Here you can find the source of durationToString(final long msec)
public static String durationToString(final long msec)
//package com.java2s; /*/*from ww w . j a v a 2 s .com*/ * Copyright 2007 Joachim Sauer * Copyright 2002-2006 Chriss Veness (vincenty formula in distance()) * * This file is part of bbTracker. * * bbTracker is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. * * bbTracker is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ public class Main { public static String durationToString(final long msec) { // i do hope no one uses bbTracker do record tracks that // are longer than Integer.MAX_VALUE seconds. final StringBuffer sb = new StringBuffer(8); int sec = (int) (msec / 1000); if (sec > 60 * 60) { final int hours = sec / (60 * 60); sb.append(hours); sec -= hours * (60 * 60); sb.append(':'); } final int minutes = sec / 60; if (sb.length() > 0) { appendTwoDigits(sb, minutes, '0'); } else { sb.append(minutes); } sb.append(':'); sec -= minutes * 60; appendTwoDigits(sb, sec, '0'); return sb.toString(); } private static StringBuffer appendTwoDigits(final StringBuffer buf, final int value, final char c) { if (value < 10) { buf.append(c); } buf.append(value); return buf; } }