Here you can find the source of formatTime(final double theTime)
Parameter | Description |
---|---|
theTime | time to format in seconds. |
public static String formatTime(final double theTime)
//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); } }