Data d = new Date(); long x = d.getTime(); |
will set x to the number of milliseconds since midnight January 1, 1970 GMT.
You will use this feature to make a Java program wait for 5 second (= 5000 milliseconds) before the program finishes.
Here is the Java program that you need to complete:
import java.util.Date ;
public class myProg
{
public static void main(String[] args)
{
Date d;
// Print out the time out when the program starts
d = new Date();
System.out.println("Starting time of program is: " + d);
// Write code here to make the program wait for 5 seconds (= 5000 msec)
// Print out the time out when the program ends
d = new Date();
System.out.println("Endtime time of program is: " + d);
}
}
|
If your program is correct, it will print out messages like this:
Starting time of program is: Thu Apr 20 13:31:29 EDT 2023 Endtime time of program is: Thu Apr 20 13:31:34 EDT 2023 |
The Endtime is exactly 5 second later than the Starting time
Hint: use a while loop and keep reading the current time inside the loop body !