Java TimeUnit Usage prettyMillis(long mil)

Here you can find the source of prettyMillis(long mil)

Description

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

License

Open Source License

Parameter

Parameter Description
mil Time in milliseconds

Return

String in format hh:mm:dd.sss

Declaration

public final static String prettyMillis(long mil) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2012 Stefan Profanter.//from   w  w  w  .java 2  s .c  o m
 * 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
 ******************************************************************************/

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

  1. parseSecondsFromEpoch(Double d)
  2. parseStringDate(String date)
  3. parseTimeMillis(String timeWithOrWithoutUnit)
  4. parseUntilTime(byte[] untilTime)
  5. pause()
  6. printTimeMap(Map timeMap)
  7. printTimeString(long time)
  8. printTiming(long start, long end)
  9. randomNanos(Random rand, int decimalDigits)