Java Duration Format formatDuration(long duration, boolean simplify, boolean includeMillies)

Here you can find the source of formatDuration(long duration, boolean simplify, boolean includeMillies)

Description

format Duration

License

Open Source License

Declaration

public static String formatDuration(long duration, boolean simplify, boolean includeMillies) 

Method Source Code

//package com.java2s;
/*/*from w w  w.  j ava  2 s .c o  m*/
 * (c) Copyright 2007-2013 by Volker Bergmann. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, is permitted under the terms of the
 * GNU General Public License.
 *
 * For redistributing this software or a derivative work under a license other
 * than the GPL-compatible Free Software License as defined by the Free
 * Software Foundation or approved by OSI, you must first obtain a commercial
 * license to this software product from Volker Bergmann.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
 * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
 * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

public class Main {
    public static String formatDuration(long duration, boolean simplify, boolean includeMillies) {
        // calculate parts
        int hours = (int) (duration / 3600000);
        duration -= hours * 3600000;
        int minutes = (int) (duration / 60000);
        duration -= 60000 * minutes;
        int seconds = (int) (duration / 1000);
        int millis = (int) (duration - seconds * 1000);
        // format string
        if (simplify)
            return formatSimplified(hours, minutes, seconds, millis, includeMillies);
        else
            return formatFixed(hours, minutes, seconds, millis, includeMillies);
    }

    private static String formatSimplified(int hours, int minutes, int seconds, int millis,
            boolean includeMillies) {
        StringBuilder builder = new StringBuilder();
        String unit = null;
        if (hours > 0) {
            builder.append(hours);
            unit = " h";
        }
        if (minutes > 0 || seconds > 0 || (includeMillies && millis > 0)) {
            if (unit != null) {
                builder.append(':');
                if (minutes < 10)
                    builder.append('0');
            }
            if (unit != null || minutes > 0) {
                builder.append(minutes);
                if (minutes > 0 && unit == null)
                    unit = " min";
            }
            if (seconds > 0 || (includeMillies && millis > 0)) {
                if (unit != null) {
                    builder.append(':');
                    if (seconds < 10)
                        builder.append('0');
                }
                builder.append(seconds);
                if (unit == null)
                    unit = " s";
                if (includeMillies)
                    appendMillis(millis, builder);
            }
        } else if (builder.length() == 0) {
            builder.append("0");
            unit = " s";
        }

        builder.append(unit);
        return builder.toString();
    }

    private static String formatFixed(int hours, int minutes, int seconds, int millis, boolean includeMillies) {
        StringBuilder builder = new StringBuilder();
        builder.append(hours).append(':');
        if (minutes < 10)
            builder.append('0');
        builder.append(minutes);
        builder.append(':');
        if (seconds < 10)
            builder.append('0');
        builder.append(seconds);
        if (includeMillies)
            appendMillis(millis, builder);
        builder.append(" h");
        return builder.toString();
    }

    private static void appendMillis(int millis, StringBuilder builder) {
        builder.append(".");
        if (millis < 100)
            builder.append('0');
        if (millis < 10)
            builder.append('0');
        builder.append(millis);
    }
}

Related

  1. formatDuration(long duration)
  2. formatDuration(long duration)
  3. formatDuration(long duration)
  4. formatDuration(long duration)
  5. formatDuration(long duration, boolean displayMilliseconds)
  6. formatDuration(long durationInMs)
  7. formatDuration(long durationMillis)
  8. formatDuration(long durationSec)
  9. formatDuration(long elapsed)