Java Duration Format formatDurationHMSms(long ms)

Here you can find the source of formatDurationHMSms(long ms)

Description

format Duration HM Sms

License

Open Source License

Declaration

public static String formatDurationHMSms(long ms) 

Method Source Code

//package com.java2s;
/*//from   ww  w  . j a  va2s .c  om
 * Robonobo Common Utils
 * Copyright (C) 2008 Will Morton (macavity@well.com) & Ray Hilton (ray@wirestorm.net)
    
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
    
 * This program 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

public class Main {
    public static String formatDurationHMSms(long ms) {
        long hrs = ms / 1000 / 60 / 60;
        long min = ms / 1000 / 60 % 60;
        long sec = ms / 1000 % 60;
        long rms = ms % 1000;

        return leftPad(String.valueOf(hrs), 2, '0') + ":" + leftPad(String.valueOf(min), 2, '0') + ":"
                + leftPad(String.valueOf(sec), 2, '0') + ":" + leftPad(String.valueOf(rms), 3, '0');
    }

    public static String leftPad(String str, int size) {
        return leftPad(str, size, ' ');
    }

    public static String leftPad(String str, int size, char padChar) {
        if (str.length() >= size)
            return str;
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < size - str.length(); i++) {
            sb.append(padChar);
        }
        sb.append(str);
        return sb.toString();
    }
}

Related

  1. formatDuration(long seconds)
  2. formatDuration(long seconds)
  3. formatDuration(long t_millis)
  4. formatDuration(long timeInMillis)
  5. formatDurationAsTime(final int duration)
  6. formatDurationLpad(final String s)
  7. formatDurationMills(long millis)
  8. formatDurationOneUnit(long milis)
  9. formatHuman(Duration duration)