The System class holds a collection of static methods and variables. This class handles all the activities regarding the standard input and output. The standard input, output, and error output of the java runtime are store in the in, out, err variables. Some of the methods defined in the system class are as follows.

void CurrentTimeMillis() – return the current time in terms of milliseconds since midnight, January 1,1970

void gc() - Initiate the garbage collector.

void exit(int code) – halts the execution and returns the value of integer to parent process usually to an operating system.

 import java.io.IOException;

public class Elapsed {

  public static void main(String[] args) throws IOException {

    long lngStart, lngEnd = 0;
    System.out.println("Timing a for from 0 to 1,000,000");
    
    //time a for loop from 0 to 1,000,000
    lngStart=System.currentTimeMillis();

    for(int j=0;j<1000000;j++)
      lngEnd = System.currentTimeMillis();

    System.out.println("Elapsed time : " + (lngEnd-lngStart));

  }

}

Output:

Timing a for from 0 to 1,000,000
Elapsed time: 78