Convert millisecond value to human readable string hh:mm:ss.xxx - Java java.util

Java examples for java.util:Millisecond

Description

Convert millisecond value to human readable string hh:mm:ss.xxx

Demo Code

/*******************************************************************************
 * Copyright (c) 2012 Stefan Profanter.//from   w  ww.  j  ava 2s . com
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     Stefan Profanter - initial API and implementation, Year: 2012
 ******************************************************************************/
//package com.java2s;
import java.util.concurrent.TimeUnit;

public class Main {
    /**
     * Convert millisecond value to human readable string hh:mm:ss.xxx
     * 
     * @param mil
     *            Time in milliseconds
     * @return String in format hh:mm:dd.sss
     */
    public final static String prettyMillis(long mil) {
        long h, m, s;
        long millis = mil;
        h = TimeUnit.MILLISECONDS.toHours(millis);
        millis -= TimeUnit.HOURS.toMillis(h);

        m = TimeUnit.MILLISECONDS.toMinutes(millis);
        millis -= TimeUnit.MINUTES.toMillis(m);

        s = TimeUnit.MILLISECONDS.toSeconds(millis);
        millis -= TimeUnit.SECONDS.toMillis(s);
        return String.format("%02d:%02d:%02d.%03d", h, m, s, millis);
    }
}

Related Tutorials