Java TimeUnit Format format(final long duration, final TimeUnit sourceUnit, final TimeUnit min)

Here you can find the source of format(final long duration, final TimeUnit sourceUnit, final TimeUnit min)

Description

format

License

Apache License

Declaration

public static String format(final long duration, final TimeUnit sourceUnit, final TimeUnit min) 

Method Source Code

//package com.java2s;
/**/*from   w w  w.  j a va2 s. co m*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.util.concurrent.TimeUnit;

public class Main {
    public static String format(final long duration, final TimeUnit sourceUnit, final TimeUnit min) {
        return format(duration, sourceUnit, min, max());
    }

    public static String format(final long duration, final TimeUnit sourceUnit) {
        return format(duration, sourceUnit, min(), max());
    }

    /**
     * Converts time to a human readable format within the specified range
     *
     * @param duration   the time to be converted
     * @param sourceUnit the unit representing this time
     * @param min        the lowest time unit of interest
     * @param max        the highest time unit of interest
     */
    public static String format(long duration, final TimeUnit sourceUnit, final TimeUnit min, final TimeUnit max) {
        final StringBuilder res = new StringBuilder();

        TimeUnit current = max;

        while (duration > 0) {
            final long temp = current.convert(duration, sourceUnit);

            if (temp > 0) {

                duration -= sourceUnit.convert(temp, current);

                res.append(temp).append(" ").append(current.name().toLowerCase());

                if (temp < 2) {
                    res.deleteCharAt(res.length() - 1);
                }

                res.append(", ");
            }

            if (current == min) {
                break;
            }

            current = TimeUnit.values()[current.ordinal() - 1];
        }

        // we never got a hit, the time is lower than we care about
        if (res.lastIndexOf(", ") < 0) {
            return "0 " + min.name().toLowerCase();
        }

        // yank trailing  ", "
        res.deleteCharAt(res.length() - 1);
        res.deleteCharAt(res.length() - 1);

        //  convert last ", " to " and"
        final int i = res.lastIndexOf(", ");
        if (i > 0) {
            res.deleteCharAt(i);
            res.insert(i, " and");
        }

        return res.toString();
    }

    private static TimeUnit max() {
        final TimeUnit[] values = TimeUnit.values();
        return values[values.length - 1];
    }

    private static TimeUnit min() {
        return TimeUnit.values()[0];
    }
}

Related

  1. formatDuration(long time, TimeUnit unit)
  2. formatHighest(long duration, final TimeUnit max)
  3. formatMillis(long duration, TimeUnit max, TimeUnit min, boolean useAbbreviation)
  4. formatMinutesSeconds(final long sourceDuration, final TimeUnit sourceUnit)