Java examples for java.util:Time Format
Gets the difference in time formatted to be easy to read
//package com.java2s; import java.text.DecimalFormat; public class Main { public static void main(String[] argv) { long start = 42; long end = 12312342; System.out.println(formatTimeDifference(start, end)); }/*from www.java 2 s .c om*/ /** * Formatter to clean up numbers */ public final static DecimalFormat NUMBER_FORMATTER_NO_DECIMALS = new DecimalFormat( "#,###"); /** * Gets the difference in time formatted to be * easy to read * * @param start - nano sec start time * @return nano sec difference up to now formatted */ public static String formatTimeDifference(long start, long end) { return formatNanoTime(end - start); } /** * Breaks nano second time long into seconds, miliseconds, microseconds, and nanosecond * sections making it easier to read. * * @param nano - long value of time * @return formatted string */ public static String formatNanoTime(long nano) { long s = nano / 1000000000; long ms = (nano % 1000000000) / 1000000; long us = ((nano % 1000000000) % 1000000) / 1000; long ns = ((nano % 1000000000) % 1000000) % 1000; String string = ""; string += fitIntoSpaces(s, 3) + "s "; string += fitIntoSpaces(ms, 3) + "ms "; string += fitIntoSpaces(us, 3) + "us "; string += fitIntoSpaces(ns, 3) + "ns "; return string; } /** * Formats a number to fit into so many spaces, and to contain , * * @param num - number to format * @param spaces - spaces to pad the front with * @return formatted string */ public static String fitIntoSpaces(long num, int spaces) { return padLeft(NUMBER_FORMATTER_NO_DECIMALS.format(num), spaces); } /** Adds padding to the left of the string * @param s - string * @param n - number of spaces * @return */ public static String padLeft(String s, int n) { return String.format("%1$" + n + "s", s); } }