Create a Digital Clock in Java

  • Last updated Apr 25, 2024

In this tutorial, we'll learn how to create a digital clock in Java that looks like the one below:


To create a digital clock, we will use JFrame, JLabel, and Timer class from the Swing framework. The JFrame class is used to construct a top-level window for a Java application. The JLabel class is used to display a string and the Timer class is used to fire events at specified intervals.

Here's a code snippet that demonstrates how to create and display a digital clock in Java:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.Timer;

public class DigitalClock {

   static void display() {
      // create window for clock
      JFrame clockFrame = new JFrame("Digital Clock");
      clockFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      // create label to print time
      JLabel timeLabel = new JLabel("", SwingConstants.CENTER);
      timeLabel.setPreferredSize(new Dimension(400, 100));
      timeLabel.setFont(new Font("Calibri", Font.BOLD, 50));
      clockFrame.getContentPane().add(timeLabel, BorderLayout.CENTER);

      //call pack() before setLocationRelativeTo ()
      //pack() determines the size of the window for centering the window
      clockFrame.pack();
      clockFrame.setLocationRelativeTo(null);
      // Display the window
      clockFrame.setVisible(true);

      int delay = 100;
      Timer timer = new Timer(delay, new ActionListener() {

          @Override
	  public void actionPerformed(ActionEvent e) {
	     LocalDateTime now = LocalDateTime.now();
	     DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
	     String formattedDateTime = now.format(formatter);
	     timeLabel.setText(formattedDateTime);
	  }
      });

      timer.start();
   }

   public static void main(String args[]) {
      display();
   }
}

This example code begins with importing necessary classes from the AWT (Abstract Window Toolkit) and Swing libraries, which are used for creating the graphical user interface components.

The class is named DigitalClock, which contains two (display and main) methods. The display() method has the main logic of the program that sets up and displays the digital clock window.

Inside the display() method, a JFrame object named clockFrame is created, representing the window for the digital clock. It is given the title "Digital Clock" and set to exit the application when closed. A JLabel object named timeLabel is created to display the time. It is configured with the appropriate font, size, and alignment settings. It is added to the center of the clockFrame using the BorderLayout.CENTER constant. The pack() method is called on the clockFrame to set its size based on the preferred size of its components. The setLocationRelativeTo(null) method is called to center the clockFrame on the screen. Finally, the clockFrame is set to be visible.

The Timer class is used to update the time displayed on the timeLabel at regular intervals. A timer is created with a specified delay of 100 milliseconds. An anonymous ActionListener is defined to handle the timer's action events, which updates the timeLabel with the current time obtained using LocalDateTime and DateTimeFormatter classes. The timer is started using the start() method. The main() method is defined, which simply calls the display() method to start the digital clock program. When you run this code, it will display a window with a digital clock showing the current time. The clock updates every 100 milliseconds to keep the displayed time accurate.