How to Create a Digital Clock in Java

By Kevin Walker

One of the most powerful features of the Java programming language is its Swing graphical user interface (GUI) library. Java users can construct visual, event-driven programs like digital clocks using only a few simple commands. Java programmers have a plethora of components available, such as labels, buttons and timers, which they plug together to assemble their programs.

Step 1

Create a DigitalClock class. It needs to extend the JFrame class that comes with the Swing user interface library in the Java Develoment Kit from Sun Microsystems. It will also implement the ActionListener interface to enable it to respond to the timer event and allow the clock to update itself. This can be done by pasting the following code into a file named \"\" import java.awt.FontGOimport java.awt.HeadlessExceptionGOimport java.awt.event.ActionEventGOimport java.awt.event.ActionListenerGOimport java.text.SimpleDateFormatGOimport java.util.DateGOimport javax.swing.JFrameGOimport javax.swing.JLabelGOimport javax.swing.TimerGO/** * This class displays a digital clock on the screen. * @author Kevin Walker */public class DigitalClock extends JFrame implements ActionListener { // All other steps should have their code added here.}If you are using a dedicated Java Development Environment such as Netbeans or Eclipse, then there will be an option in the File menu to do this automatically for you.

Step 2

Create a JLabel to display the current time to the user using the following command: JLabel timeLabel = new JLabel();

Step 3

Initialize the format that your digital clock will have using the SimpleDateFormat class from the Java library. SimpleDateFormat sdf = new SimpleDateFormat(\"HH:mm:ss\")GOThis format will display the current hour, minute and seconds for the user. A full listing of format codes is available in the SimpleDateFormat Javadoc on the Sun Microsystems website (see References).

Step 4

Create a timer. This object will not hold the time, but rather will function as an alarm clock that tells the program to update the current time regularly. To do this, paste the following code: Timer timer;

Step 5

Create a constructor method to build the program by pasting the following: public digitalClock() { // Run the constuctor for the parent JFrame class. super()GO // Exit the program when the user closes the window. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)GO // Get the current system time and display it // according to the format we defined. timeLabel.setText(sdf.format(new Date(System.currentTimeMillis())))GO // Set a larger, nicer font for the digital clock than the default. timeLabel.setFont(new Font(\"Dialog\", Font.PLAIN, 24))GO // Set the timer to update the clock every 500 milliseconds (0.5 seconds) timer = new Timer(500, this)GO timer.setRepeats(true)GO timer.start()GO // Add the time display to the window and make it visible. this.add(timeLabel)GO this.pack()GO this.setVisible(true)GO }

Step 6

Update the clock with the new system time whenever the timer goes off: public void actionPerformed(ActionEvent e) { // If the timer caused this event. if (e.getSource().equals(timer)) { // Then set a new time. timeLabel.setText(sdf.format(new Date(System.currentTimeMillis())))GO } }

Step 7

Create an entry point for the digital clock program from the operating system by giving it a main method. public static void main(String[] args) { // Create the digital clock. new digitalClock()GO }