Java Program to Design a Digital Clock

You are currently viewing Java Program to Design a Digital Clock

Java program to design a digital clock

// Java program to design a digital clock

public class Main {
  public static void main(String[] args) {
    int hour = 0;
    int minute = 0;
    int second = 0;

    try {
      while (true) {
        //clear output screen
        System.out.print("\033[H\033[2J");
        System.out.flush();

        System.out.printf("%02d : %02d : %02d ", hour, minute, second);

        second++;

        if (second == 60) {
          minute += 1;
          second = 0;
        }
        if (minute == 60) {
          hour += 1;
          minute = 0;
        }
        if (hour == 24) {
          hour = 0;
          minute = 0;
          second = 0;
        }

        Thread.sleep(1000);
      }
    } catch (Exception e) {
      System.out.println(e);
    }
  }
}

Output

00 : 00 : 05

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply