Java Program to Get Elapsed Time in Seconds and Milliseconds

Java program to get elapsed time in seconds and milliseconds

//Java program to get elapsed time in seconds and milliseconds.
 
import java.util.Scanner;
 
public class ElapsedTime 
{
 
    public static void main(String[] args) 
    {
        long startTime; //start time
        long endTime;   //end time
        double time;    //time difference
         
        startTime = System.currentTimeMillis();
 
        //doing some operation
        //read and print your name      
        System.out.print("Enter you name: ");
        //Scanner class object
        Scanner SC=new Scanner(System.in);
        String name=SC.nextLine();
         
        System.out.println("Thanks "+ name +" ! ");
 
        endTime = System.currentTimeMillis();
        time = (endTime - startTime) / 1000.0;
 
        System.out.println("\nElapsed Time is:  " + time);
    }
 
}

Output

    
    Enter you name: Alexender
    Thanks Alexender ! 

    Elapsed Time is:  3.452

Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment