How to Create a digital watch using Java

In this program, we are going to create a digital watch with the swing in java. In this program, we will use these Java packages:

  • swing package
  •  awt package
  • A util package
  • The text package

First, of all, we have to import the swing package for creating a frame. After that, we will use the util package for creating the user interface. We also use a text package for writing the program in which date-time is mandatory.

Java program to create a digital watch

Below is our program:

import javax.swing.*;  
import java.awt.*;  
import java.text.*;  
import java.util.*;  
public class DigitalWatch implements Runnable{  
JFrame f;  
Thread t=null;  
int hours=0, minutes=0, seconds=0;  
String timeString = "";  
JButton b;  
  
DigitalWatch(){  
    f=new JFrame();  
      
    t = new Thread(this);  
        t.start();  
      
    b=new JButton();  
        b.setBounds(100,100,100,50);  
      
    f.add(b);  
    f.setSize(300,400);  
    f.setLayout(null);  
    f.setVisible(true);  
}  
  
 public void run() {  
      try {  
         while (true) {  
  
            Calendar cal = Calendar.getInstance();  
            hours = cal.get( Calendar.HOUR_OF_DAY );  
            if ( hours > 12 ) hours -= 12;  
            minutes = cal.get( Calendar.MINUTE );  
            seconds = cal.get( Calendar.SECOND );  
  
            SimpleDateFormat formatter = new SimpleDateFormat("hh:mm:ss");  
            Date date = cal.getTime();  
            timeString = formatter.format( date );  
  
            printTime();  
  
            t.sleep( 1000 );  // interval given in milliseconds  
         }  
      }  
      catch (Exception e) { }  
 }  
  
public void printTime(){  
b.setText(timeString);  
}  
  
public static void main(String[] args) {  
    new DigitalWatch();  
          
  
}  
}  

First, we create a frame and set the size of the frame. After that, we initialize three variables Hours, Minutes, Seconds and set all the values of the variables zero. We use the Calendar class in this program. The Java Calendar class is the abstract class that provides us the method of date, time, year and month.

The main logic of the program

while (true) {  
  
            Calendar cal = Calendar.getInstance();  
            hours = cal.get( Calendar.HOUR_OF_DAY );  
            if ( hours > 12 ) hours -= 12;  
            minutes = cal.get( Calendar.MINUTE );  
            seconds = cal.get( Calendar.SECOND );  
  
            SimpleDateFormat formatter = new SimpleDateFormat("hh:mm:ss");  
            Date date = cal.getTime();  
            timeString = formatter.format( date );  
  
            printTime();  
  
            t.sleep( 1000 );  // interval given in milliseconds  
         }  
      }

We also use Sleep(1000) it gives the interval in milliseconds.

The output of the program

The output of the program will be looks like you can see in the image below:

Create a digital watch using Java

So we did it successfully.

Also, read:

 

Leave a Reply

Your email address will not be published. Required fields are marked *