Java Long Number to Time convertTimeToString(final long time)

Here you can find the source of convertTimeToString(final long time)

Description

Converting the time into String format.

License

Open Source License

Parameter

Parameter Description
time The time in millisecs calculates since 1970.

Return

The String representation of the time: seconds.millisecs.

Declaration

public static String convertTimeToString(final long time) 

Method Source Code

//package com.java2s;
/**/*from  w w  w.j a  v a  2 s .c o m*/
 * This file is part of Everit Test Runner Bundle.
 *
 * Everit Test Runner Bundle is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Everit Test Runner Bundle 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Everit Test Runner Bundle.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * The number that should be used to get the seconds from a millisec based value during a diviation.
     */
    private static final int MILLISEC_DECIMAL_DIVIDER = 1000;
    /**
     * The smallest number that needs three digits.
     */
    private static final int SMALLEST_THREE_DIGIT_DECIMAL = 100;
    /**
     * The smallest number that needs two digit.
     */
    private static final int SMALLEST_TWO_DIGIT_DECIMAL = 10;

    /**
     * Converting the time into String format.
     * 
     * @param time
     *            The time in millisecs calculates since 1970.
     * @return The String representation of the time: seconds.millisecs.
     */
    public static String convertTimeToString(final long time) {
        StringBuilder sb = new StringBuilder();
        sb.append(time / MILLISEC_DECIMAL_DIVIDER);
        long millisecs = time % MILLISEC_DECIMAL_DIVIDER;
        if (millisecs > 0) {
            sb.append(".");
            if (millisecs < SMALLEST_TWO_DIGIT_DECIMAL) {
                sb.append("00");
            } else if (millisecs < SMALLEST_THREE_DIGIT_DECIMAL) {
                sb.append("0");
            }
            sb.append(millisecs);
        }

        return sb.toString();
    }
}

Related

  1. convertTime(long value)
  2. convertTime(long x)
  3. convertTimestampToSec(long timestampInMs)
  4. convertTimestampToUTCString(final long aTimestamp)
  5. convertTimeToInt(long seconds)
  6. convertTimeToString(final long timeInMillis)
  7. convertTimeUnit(long epochTime, String timeUnit)
  8. convertTimeUnit(long epochTime, String timeUnit)
  9. convertUnixTime(long unixtime)