The do while loop will execute for a 30 sec duration.With in that I have to print the current date in every 5 sec... For that I have written a code as below. But it is not working as expected...
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
long duration = (30 * 1000);
do {
while (true) {
try {
System.out.println(" Date: " + new Date());
Thread.sleep(2 * 1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} while ((System.currentTimeMillis() - startTime) < duration);
}