Java TimeUnit Usage durationToString(long duration)

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

Description

Convert duration to human friendly format.

License

Open Source License

Declaration

@SuppressWarnings("nls")
private static String durationToString(long duration) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2012, 2016 Andrew Gvozdev and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from  w  w  w. j a v a 2 s.  c  om*/
 *     Andrew Gvozdev - initial API and implementation
 *     IBM Corporation
 *******************************************************************************/

import java.util.concurrent.TimeUnit;

public class Main {
    /**
     * Convert duration to human friendly format.
     */
    @SuppressWarnings("nls")
    private static String durationToString(long duration) {
        String result = "";
        long days = TimeUnit.MILLISECONDS.toDays(duration);
        if (days > 0) {
            result += days + "d,";
        }
        long hours = TimeUnit.MILLISECONDS.toHours(duration) % 24;
        if (hours > 0) {
            result += hours + "h:";
        }
        long minutes = TimeUnit.MILLISECONDS.toMinutes(duration) % 60;
        if (minutes > 0) {
            result += minutes + "m:";
        }
        long seconds = TimeUnit.MILLISECONDS.toSeconds(duration) % 60;
        if (seconds > 0) {
            result += seconds + "s.";
        }
        long milliseconds = TimeUnit.MILLISECONDS.toMillis(duration) % 1000;
        result += milliseconds + "ms";

        return result;
    }
}

Related

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