Thu Sep 23 2021

Timer

File Name: timer.java

import java.io.*;

class time extends Thread {
	int hour = 0, minute = 0, second = 0, i;
	String hr, min, sec;
	public void run() {
		while(second != 61) {
			second++;
			if(second == 60) {
				minute = minute + 1;
				second = 0;
			}
			if(minute == 60) {
				hour = hour + 1;
				minute = 0;
			}
			if(hour == 24)
				hour = 0;

			/* Convert integer into string on conditional expression */
			sec = (second < 10) ? "0"+Integer.toString(second) : Integer.toString(second);
			min = (minute < 10) ? "0"+Integer.toString(minute) : Integer.toString(minute);
			hr = (hour < 10) ? "0"+Integer.toString(hour) : Integer.toString(hour);
			System.out.print(hr+":"+min+":"+sec+"\b\b\b\b\b\b\b\b");
			try {

				/* Sleep the process for 1 second */
				sleep(1000);
			}
			catch(Exception e) {
				System.out.println(e);
			}
		}
	}
}

class timer {
	public static void main(String args[ ]) {
		time x = new time();
		x.start();
	}
}



/* Output */
00:00:40

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.