Java Time Format formatTimeLikeTimer(long time, boolean appendMs)

Here you can find the source of formatTimeLikeTimer(long time, boolean appendMs)

Description

Formats an long integer time value in format (-)hh:mm:ss(.S)

License

Open Source License

Parameter

Parameter Description
time Long value to convert into a time string

Return

A time string in the format hh:mm:ss

Declaration

public static String formatTimeLikeTimer(long time, boolean appendMs) 

Method Source Code

//package com.java2s;
/**/* w w w.j  ava  2  s  .c  o m*/
 * Copyright (C) 2014 Aniruddh Fichadia
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 * If you use or enhance the code, please let me know using the provided author information or via
 * email Ani.Fichadia@gmail.com.
 */

public class Main {
    /**
     * Formats an long integer time value in format (-)hh:mm:ss(.S)
     * 
     * @param time Long value to convert into a time string
     * 
     * @return A time string in the format hh:mm:ss
     */
    public static String formatTimeLikeTimer(long time, boolean appendMs) {
        boolean isPositive = (time >= 0);

        if (!isPositive) {
            time *= -1;
        }

        long milliSeconds = time;
        long seconds = time / 1000;
        long minutes = seconds / 60;
        long hours = minutes / 60;

        milliSeconds = milliSeconds % 1000;
        seconds = seconds % 60;
        minutes = minutes % 60;
        hours = hours % 24;

        String msTrim = (milliSeconds + "").charAt(0) + "";

        return ((isPositive) ? "" : "-") + hours + ":" + ((minutes < 10) ? "0" + minutes : minutes) + ":"
                + ((seconds < 10) ? "0" + seconds : seconds) + (appendMs ? "." + msTrim : "");
    }

    /**
     * Refer to {@link #formatTimeLikeTimer(long, boolean)}. Supplies boolean argument as true
     */
    public static String formatTimeLikeTimer(long time) {
        return formatTimeLikeTimer(time, true);
    }
}

Related

  1. formatTimeInMilisec(long time)
  2. formatTimeInNanos(long time)
  3. formatTimeInterval(final long time)
  4. formatTimeInterval(long in)
  5. formatTimeLength(long ms)
  6. formatTimeNicely(long millis)
  7. formatTimeOffset(long offset)
  8. formatTimePart2(long number, String unit)
  9. formatTimePeriod(long millis)