Here you can find the source of formatTime(long dt, TimeUnit input, TimeUnit output, int decimalPlaces)
Parameter | Description |
---|---|
dt | the time difference |
input | the unit of <code>dt</code> |
output | the desired output unit of <code>dt</code> |
decimalPlaces | the number of decimal places to print or -1 to print all in precision |
public static String formatTime(long dt, TimeUnit input, TimeUnit output, int decimalPlaces)
//package com.java2s; /*// ww w .ja v a 2 s . c o m * Copyright (c) 2013 Game Salutes. * 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.html * * Contributors: * Game Salutes - Repackaging and modifications of original work under University of Chicago and Apache License 2.0 shown below * * Repackaging from edu.uchicago.nsit.iteco.utils to com.gamesalutes.utils * * Copyright 2008 - 2011 University of Chicago * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. ******************************************************************************/ import java.util.concurrent.TimeUnit; public class Main { /** * Returns a formatted time difference string * @param dt the time difference * @param input the unit of <code>dt</code> * @param output the desired output unit of <code>dt</code> * @param decimalPlaces the number of decimal places to * print or -1 to print all in precision * @return the formatted time string */ public static String formatTime(long dt, TimeUnit input, TimeUnit output, int decimalPlaces) { if (input == null) throw new NullPointerException("unit"); if (output == null) throw new NullPointerException("output"); double inputMult; double outputMult; String strOutUnit; switch (input) { case MICROSECONDS: inputMult = 1.0e-6; break; case MILLISECONDS: inputMult = 1.0e-3; break; case NANOSECONDS: inputMult = 1.0e-9; break; case SECONDS: inputMult = 1.0; break; default: throw new IllegalArgumentException("input=" + input); } switch (output) { case MICROSECONDS: outputMult = 1.0e-6; strOutUnit = "us"; break; case MILLISECONDS: outputMult = 1.0e-3; strOutUnit = "ms"; break; case NANOSECONDS: outputMult = 1.0e-9; strOutUnit = "ns"; break; case SECONDS: outputMult = 1.0; strOutUnit = "s"; break; default: throw new IllegalArgumentException("output=" + output); } StringBuilder str = new StringBuilder(64); double result = dt * inputMult / outputMult; String strResult = String.format("%" + (decimalPlaces >= 0 ? "." + decimalPlaces : "") + "f", result); str.append(strResult).append(' ').append(strOutUnit); return str.toString(); } /** * Returns the <code>String</code> form of <code>o</code>. Unlike * <code>String.valueOf(Object)</code>, if <code>o</code> is <code>null</code> * an empty <code>String</code> will be returned. * * @param o the <code>Object</code> * @return string form of <code>o</code> */ public static String toString(Object o) { return o != null ? o.toString() : ""; } }