How to render time in the maven format

Call the following method:

	String.format( "[ %,.3f s ]", value);

Example:

The following code uses System.nanoTime to get a more accurate time, then runs a loop on a Math.log function. Finally, it takes the time and renders the difference between the start time and the end time in seconds the same way Maven would render it.

public class MavenTime {

	public static void main(String[] argv){

		long startTime = System.nanoTime();

		double a = 4;
		for( int i =0; i < 100000; i++ ){
			a = a * Math.log( i );
		}
		
		long endTime = System.nanoTime();
    	System.out.println( "SUCCESS " + String.format( "[ %,.3f s ]", (float)(((endTime-startTime)  ) /1000 )/1000 / 1000) );

	}
	
}

The output will be:

SUCCESS [ 0.004 s ]

References:

Java 8
Formatter

Share this article:

Recent Comments