Java Programming

Timer

Java programming example for timer using thread

9/23/2021
0 views
timer.javaJava
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
Java programmingtimer using threadtimer

Loading comments...

Related Examples

Deliver breaking news, insightful commentary, and exclusive reports.

Targeting readers who rely on our platform to stay ahead of the curve.

Contact Us: benzingaheadlines@gmail.com