Java TimeUnit Usage durationToString(long millis)

Here you can find the source of durationToString(long millis)

Description

Convert a long representing a ms duration into a string in the format HH:mm:ss.SSS.

License

Open Source License

Parameter

Parameter Description
millis a parameter

Declaration

public static String durationToString(long millis) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2009, 2016 GreenVulcano ESB Open Source Project.
 * All rights reserved.// w  w  w  . jav a2  s. c  om
 *
 * This file is part of GreenVulcano ESB.
 *
 * GreenVulcano ESB is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * GreenVulcano ESB 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with GreenVulcano ESB. If not, see <http://www.gnu.org/licenses/>.
 *******************************************************************************/

import java.util.concurrent.TimeUnit;

public class Main {
    /**
     * Convert a long representing a ms duration into a string in the format HH:mm:ss.SSS.
     *
     * @param millis
     * @return
     */
    public static String durationToString(long millis) {
        long h = TimeUnit.MILLISECONDS.toHours(millis);
        millis -= TimeUnit.HOURS.toMillis(h);
        long m = TimeUnit.MILLISECONDS.toMinutes(millis);
        millis -= TimeUnit.MINUTES.toMillis(m);
        long s = TimeUnit.MILLISECONDS.toSeconds(millis);
        millis -= TimeUnit.SECONDS.toMillis(s);
        return String.format("%d:%02d:%02d.%03d", h, m, s, millis);
    }
}

Related

  1. daysFromToday(long toDate)
  2. daysToMillis(int days)
  3. durationInSecs(long startNanos, long endNanos)
  4. durationIsValid(long seconds, int nanos)
  5. durationToString(long duration)
  6. elapsedMicroSec(long startNanoTime)
  7. elapsedTime(long start, long end)
  8. elapsedTimeSince(Date d)
  9. format(long elapsed, boolean hours)