Coders Packet

Digital Clock using Java Swing

By Arya Bhattacharyya

This is a very simple project using Java Swing. It shows the Indian Standard Time (IST) - UTC + 5:30 in real time in a separate window.

Explanation:

All the packages used in this project are as follows:

import javax.swing.*;
import java.awt.*;
import java.util.Date;

A window is created with a specific default size and location. A font is set to be used by all the labels in the window.

 

The Timer class, present in the javax.swing package is used to cause an event every 1000 ms or 1 second, which updates the clock. A lambda expression is used to create the digital clock as follows:

Timer timer = new Timer(1000, e -> {
    String dateTime = new Date().toLocaleString();
    label.setText(dateTime);
});

Other necessary labels are added. The main() method is used to build and run the application.

 

Result:

Digital Clock Output

Download project

Reviews Report

Submitted by Arya Bhattacharyya (xxEasterGrymm)

Download packets of source code on Coders Packet