Accepted answer

You can compare times using System.nanoTime() . It will return the time in nanoseconds.

Returns the current value of the most precise available system timer, in nanoseconds.

You could use it like this:

long startTime = System.nanoTime();

// code

long endTime = System.nanoTime();
System.out.println("Took "+(endTime - startTime) + " ns"); 

Usefull links:


This is a typical usecase for aspects, for example using Spring AOP:

public class TimerAspect {

    private static final Logger LOG = Logger.getLogger(TimerAspect.class);

    public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
        long startTime = System.nanoTime();
        Object retVal = pjp.proceed();
        long endTime = System.nanoTime();"Call to %s.%s with args %s took %s ns", pjp.getTarget(), pjp.getSignature(), Arrays.toString(pjp.getArgs()), endTime - startTime));
        return retVal;

And in your application context:

<bean id="myAspect" class=""/>   


If you just want to know how long does your program run use System.currentTimeMillis() in the beginning and end of your program.


I like using the Apache Commons StopWatch class when I have the library available.

import org.apache.commons.lang3.time.StopWatch;

// ...

StopWatch stopWatch = new StopWatch();
String message = "Task : %s (%s) seconds";

// ...

System.out.println(String.format(message, "10", stopWatch.toSplitString()));

// ...

System.out.println(String.format(message, "20", stopWatch.toSplitString()));



You can use 2 APIs provided by System class

  1. System.currentTimeMillis() If code takes time in Millisecond range
  2. System.nanoTime() If code takes time in Nanosecond range

Sample Code

public class TestTimeTaken {
    public static void main(String args[]) throws InterruptedException{
        long startTimeNanoSecond = System.nanoTime();
        long startTimeMilliSecond = System.currentTimeMillis();


        long endTimeNanoSecond = System.nanoTime();
        long endTimeMilliSecond = System.currentTimeMillis();

        System.out.println("Time Taken in "+(endTimeNanoSecond - startTimeNanoSecond) + " ns");
        System.out.println("Time Taken in "+(endTimeMilliSecond - startTimeMilliSecond) + " ms");



If you have Spring as a dependency of your project (or don't mind adding it), you can use StopWatch. As the name suggests, once initialized it will count the time until you stop it. You can then check the time taken for a task. Multiple StopWatches can be used simultaneously to multiple tasks keeping the code clean.

Besides keeping your code clean, StopWatches help with formatting the time and other utility methods.

public void myMethod(){
    StopWatch stopWatch = new StopWatch();

    // ...
    // Do my thing
    // ...
    System.out.println("Task executed in " + stopWatch.getTotalTimeSeconds() + "s");


There is no built-in way to see for how long your program has been running. However, you could at the start of the program just store the current time, so that sometime later you can see how much time has elapsed.

public class MyProgram {
    private static long startTime = System.currentTimeMillis();

    public static void main(String[] args) {
        // Do stuff...

        // At the end
        long endTime = System.currentTimeMillis();
        System.out.println("It took " + (endTime - startTime) + " milliseconds");

Related Query

More Query from same tag