ADVERTISEMENT
ADVERTISEMENT

Java program to design a digital clock

Write a Java program to design a digital clock.
Submitted by Nidhi, on March 04, 2022

Problem Solution:

In this program, we will design a digital clock and print time.

Program/Source Code:

The source code to design a digital clock is given below. The given program is compiled and executed successfully.

// 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

Explanation:

In the above program, we created a public class Main. It contains a static method main().

The main() method is an entry point for the program. Here, we printed the digital clock by updating the second value every 1 second.

Java Basic Programs »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.