Write a program that uses a timer to print the current time once a second. Hint: The following
code prints the current time: Date now = new Date(); System.out.println(now); The Date class is
in the java.util package.
Solution
Answer -
import java.util.*;
import java.text.*;
public class DemoDate
{
public static void main(String args[])
{
int flag=1;
do
{
try
{
Date now = new Date();
System.out.println(now);
Thread.sleep(1000); //1000 milliseconds is one second.
}
catch(InterruptedException ex)
{
flag=0;
Thread.currentThread().interrupt();
}
}while(flag!=0);
}
}
Write a program that uses a timer to print the current time once a sec.docx

Write a program that uses a timer to print the current time once a sec.docx

  • 1.
    Write a programthat uses a timer to print the current time once a second. Hint: The following code prints the current time: Date now = new Date(); System.out.println(now); The Date class is in the java.util package. Solution Answer - import java.util.*; import java.text.*; public class DemoDate { public static void main(String args[]) { int flag=1; do { try { Date now = new Date(); System.out.println(now); Thread.sleep(1000); //1000 milliseconds is one second. } catch(InterruptedException ex) { flag=0; Thread.currentThread().interrupt(); } }while(flag!=0); } }