Java Time Format formatSignificantElapsedTime(final long seconds)

Here you can find the source of formatSignificantElapsedTime(final long seconds)

Description

Print only the most significant portion of the time.

License

Open Source License

Parameter

Parameter Description
seconds number of elapsed seconds NOT milliseconds.

Return

formatted string

Declaration

public static String formatSignificantElapsedTime(final long seconds) 

Method Source Code

//package com.java2s;
/*//from ww  w .  j  a v  a2s  .  co m
 * Copyright (c) 2004 Stephan D. Cote' - All rights reserved.
 * 
 * This program and the accompanying materials are made available under the 
 * terms of the MIT License which accompanies this distribution, and is 
 * available at http://creativecommons.org/licenses/MIT/
 *
 * Contributors:
 *   Stephan D. Cote 
 *      - Initial API and implementation
 */

public class Main {
    /**
     * Print only the most significant portion of the time.
     *
     * <p>This is the two most significant units of time. Form will be something
     * like "3h 26m" indicating 3 hours 26 minutes and some insignificant number
     * of seconds. Formats are Xd Xh (days-hours), Xh Xm (Hours-minutes), Xm Xs
     * (minutes-seconds) and Xs (seconds).</p>
     *
     * @param seconds number of elapsed seconds NOT milliseconds.
     *
     * @return formatted string
     */
    public static String formatSignificantElapsedTime(final long seconds) {
        final long days = seconds / 86400;
        final StringBuffer buffer = new StringBuffer();

        if (days > 0) // Display days and hours
        {
            buffer.append(days);
            buffer.append("d ");
            buffer.append(((seconds / 3600) % 24)); // hours
            buffer.append("h");

            return buffer.toString();
        }

        final int hours = (int) ((seconds / 3600) % 24);

        if (hours > 0) // Display hours and minutes
        {
            buffer.append(hours);
            buffer.append("h ");
            buffer.append(((seconds / 60) % 60)); // minutes
            buffer.append("m");

            return buffer.toString();
        }

        final int minutes = (int) ((seconds / 60) % 60);

        if (minutes > 0) // Display minutes and seconds
        {
            buffer.append(minutes);
            buffer.append("m ");
            buffer.append((seconds % 60)); // seconds
            buffer.append("s");

            return buffer.toString();
        }

        final int secs = (int) (seconds % 60);
        buffer.append(secs); // seconds
        buffer.append("s");

        return buffer.toString();

    }
}

Related

  1. formatPeroid(long time)
  2. formatRemainingTime(float seccounds)
  3. formatRuntime(long runtime)
  4. formatRuntime(long time, boolean fixedlength)
  5. formatSample(final int aValue, final long aTimestamp)
  6. formatStartRowKeyString(String id, String timestampStart)
  7. formatStrDateTime(String stringDateTime)
  8. formatString(String agentId, long agentStartTime, long transactionSequence)
  9. formattedStats(String pExecTime, int pTxns)