Saturday, 10 June 2017

Java Tutorial: System class in java [How to measure the time interval] ~ foundjava


Click here to watch in Youtube :
https://www.youtube.com/watch?v=4l6NIQyW_So&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
Java Tutorial: System class in java [How to measure the time interval] 
TimeDemo.java
import java.io.IOException;

public class TimeDemo
{

    public static void main(String[] args) throws IOException
    {
        /*
         * Returns the current value of the system timer, in
         * nanoseconds
         */
        System.out.print("time in nanoseconds = ");
        System.out.println(System.nanoTime());

        /*
         * Returns the current value of the system timer, in
         * milliseconds
         */
        System.out.print("time in milliseconds = ");
        System.out.println(System.currentTimeMillis());

    }
}
Output
time in nanoseconds = 8765309144714
time in milliseconds = 1488772824057
TimeDiffDemo.java
public class TimeDiffDemo
{

    public static void main(String[] args) throws InterruptedException
    {
        long startTime = System.currentTimeMillis();

        int i = 1;
        while (i < 1000)
        {
            Thread.sleep(10);
            ++i;
        }

        long endTime = System.currentTimeMillis();

        long timeDiff = endTime - startTime;

        System.out.println("timeDiff in ms = " + timeDiff);

    }
Output
timeDiff in ms = 10428
Click the below link to download the code:

CLICK HERE

No comments:

Post a Comment