Current system time

ReturnMethodSummary
static longnanoTime()Returns the current value of the most precise available system timer, in nanoseconds.
static longcurrentTimeMillis()Returns the current time in milliseconds.

public class Main {

  public static void main(String args[]) {
    System.out.println(System.nanoTime());
    System.out.println(System.currentTimeMillis());
  }
}

The output:


238819728662434
1288461269875

The following code uses the System.currentTimeMillis() to check the performance of a for loop.


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

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

   start = System.currentTimeMillis(); // get starting time
    for (int i = 0; i < 9999999; i++)
      ;
    end = System.currentTimeMillis(); // get ending time

    System.out.println("Elapsed time: " + (end - start));
  }
}

The output:


Timing a for loop from 0 to 1,000,000
Elapsed time: 16
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.