Thursday, 4 May 2017

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


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:
https://sites.google.com/site/ramj2eev1/home/javabasics/SystemDemo_time_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/SystemDemo_time_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/2f1fa585daa61fc8fa0957d6f04b940eddbcf7ec/BasicJava/SystemDemo_time_App/?at=master

See also:

  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment