Java System get current time in milliseconds

Introduction

We can use currentTimeMillis() method to get current time in milliseconds.

The currentTimeMillis() method returns the current time in terms of milliseconds since midnight, January 1, 1970.

public class Main {
  public static void main(String args[]) {
    long start, end;

    System.out.println("Timing a for loop from 0 to 100,000,000");

    // time a for loop from 0 to 100,000,000    
    start = System.currentTimeMillis(); // get starting time
    for(long i=0; i < 100000000L; i++) ;
    end = System.currentTimeMillis(); // get ending time

    System.out.println("Elapsed time: " + (end-start));
  }//from  w w w .ja va  2s  .  c om
}



PreviousNext

Related