Java Locale Format formatTime(final double theTime)

Here you can find the source of formatTime(final double theTime)

Description

Formats the given time into a more readable string for printing, debugging, etc.

License

Open Source License

Parameter

Parameter Description
theTime time to format in seconds.

Return

a more readable String representation of the given time.

Declaration

public static String formatTime(final double theTime) 

Method Source Code


//package com.java2s;
/*//  w ww  .  j  av a2s. co  m
 * Copyright (c) 2013 christianr.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v3
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl-3.0.html
 * 
 * Contributors:
 *     christianr - initial API and implementation
 */

import java.text.NumberFormat;
import java.util.Locale;

public class Main {
    private static NumberFormat _myIntFormat = NumberFormat.getInstance(Locale.ENGLISH);
    static private NumberFormat _myFloatFormat = NumberFormat.getInstance(Locale.ENGLISH);

    /**
     * Formats the given time into a more readable string for printing, debugging, etc.
     * @param theTime time to format in seconds.
     * @return a more readable String representation of the given time.
     */
    public static String formatTime(final double theTime) {
        int hours = (int) (theTime / 3600);
        int minutes = (int) (theTime / 60) % 60;
        int seconds = (int) theTime % 60;
        int milli = (int) (theTime * 1000) % 1000;

        return nf(hours, 2) + ":" + nf(minutes, 2) + ":" + nf(seconds, 2) + "." + nf(milli, 3);
    }

    /**
     * Utility function for formatting numbers into strings.
     * @param theNumbers the numbers to format
     * @param theDigits number of digits to pad with zeroes
     * @return String array of the formated numbers
     */
    static public String[] nf(final int[] theNumbers, int theDigits) {
        String[] formatted = new String[theNumbers.length];
        for (int i = 0; i < formatted.length; i++) {
            formatted[i] = nf(theNumbers[i], theDigits);
        }
        return formatted;
    }

    /**
     * Utility function for formatting numbers into strings.
     * @param theNumber the number to format
     * @param theDigits number of digits to pad with zeroes
     * @return formated String presentation of the number
     */
    static public String nf(final int theNumber, final int theDigits) {
        _myIntFormat.setGroupingUsed(false);
        _myIntFormat.setMinimumIntegerDigits(theDigits);
        return _myIntFormat.format(theNumber);
    }

    /**
     * Utility function for formatting numbers into strings.
     * @param theNumbers the numbers to format
     * @param theLeftDigits number of digits to the left of the decimal point
     * @param theRightDigits number of digits to the right of the decimal point
     * @return String array of the formated numbers
     */
    static public String[] nf(final float[] theNumbers, final int theLeftDigits, final int theRightDigits) {
        String[] formatted = new String[theNumbers.length];
        for (int i = 0; i < formatted.length; i++) {
            formatted[i] = nf(theNumbers[i], theLeftDigits, theRightDigits);
        }
        return formatted;
    }

    /**
     * Utility function for formatting numbers into strings.
     * @param theNumber the number to format
     * @param theLeftDigits number of digits to the left of the decimal point
     * @param theRightDigits number of digits to the right of the decimal point
     * @return String presentation of the number
     */
    static public String nf(final float theNumber, final int theLeftDigits, final int theRightDigits) {
        _myFloatFormat.setGroupingUsed(false);

        if (theLeftDigits != 0)
            _myFloatFormat.setMinimumIntegerDigits(theLeftDigits);
        if (theRightDigits != 0) {
            _myFloatFormat.setMinimumFractionDigits(theRightDigits);
            _myFloatFormat.setMaximumFractionDigits(theRightDigits);
        }
        return _myFloatFormat.format(theNumber);
    }
}

Related

  1. formatString2Date(String dateString)
  2. formattedFromDouble(double d, int scale, Locale locale)
  3. formattedFromLong(long l, Locale locale)
  4. formattedToDouble(String str, Locale loc)
  5. formatTime(Calendar calendar, String format)
  6. formatTo(StringBuilder buf, int[] d, String sep)
  7. formatToString(double num, int decPlaces)
  8. formatValues(double x, double y)
  9. formatZahl(Number nZahl, int iNachkommastellen, Locale locale)