Java Duration Format durationToString(final long msec)

Here you can find the source of durationToString(final long msec)

Description

duration To String

License

Open Source License

Declaration

public static String durationToString(final long msec) 

Method Source Code

//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;
    }
}

Related

  1. duration(int len)
  2. durationAsString(long duration)
  3. durationString(long duration)
  4. durationToFormattedString(final Duration duration)
  5. durationToSec(int millis)
  6. durationToString(int millis)
  7. durationToString(long durationInMilliSeconds)
  8. durationToString(long durationInMillisLong)
  9. durationToString(long durationMs)