Register Now

Login


Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Stopwatch code in JAVA

Stopwatch code in JAVA – A stopwatch is a device that is used to measure the elapsed time between two events. A stopwatch program in Java is a software application that simulates the functionality of a physical stopwatch by measuring the time interval between two points in time.

The stopwatch program in Java shown above uses the System.currentTimeMillis() method to get the current time in milliseconds since the epoch (midnight on January 1, 1970). When the user starts the stopwatch by pressing ‘Enter’, the current time is recorded as the start time. When the user stops the stopwatch by pressing ‘Enter’ again, the current time is recorded as the end time. The elapsed time is then calculated as the difference between the start and end times, and is displayed in milliseconds.

Stopwatch programs in Java can be used for a variety of purposes, such as timing sporting events or measuring the performance of algorithms and processes. They may include additional features, such as the ability to pause the stopwatch or to record multiple lap times.

Stop watch code in JAVA

Stopwatch code in JAVA

import java.util.Scanner;

public class Stopwatch {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    System.out.println("Press 'Enter' to start the stopwatch, and 'Enter' again to stop it.");
    input.nextLine();

    long startTime = System.currentTimeMillis();

    input.nextLine();

    long endTime = System.currentTimeMillis();

    long elapsedTime = endTime - startTime;

    System.out.println("Elapsed time: " + elapsedTime + " milliseconds");
  }
}

This program prompts the user to press ‘Enter’ to start the stopwatch, and then to press ‘Enter’ again to stop it. The elapsed time is then calculated as the difference between the start and end times, and is displayed in milliseconds. The program uses the System.currentTimeMillis() method to get the current time in milliseconds since the epoch (midnight on January 1, 1970).

Comment ( 1 )

  1. it is ok men.

Leave a reply