Tech career with our top-tier training in Data Science, Software Testing, and Full Stack Development.
phone to 4Achievers +91-93117-65521 +91-801080-5667
Navigation Icons Navigation Icons Navigation Icons Navigation Icons Navigation Icons Navigation Icons Navigation Icons

+91-801080-5667
+91-801080-5667
Need Expert Advise, Enrol Free!!
Share this article

Beginner's Guide to Building a Digital Clock in Java

Complete Guide to Building a Digital Clock in Java: Step-by-Step Project Tutorial

Here's a simple digital clock implemented in Java using the Swing framework. The clock updates every second and displays the current time in hours, minutes, and seconds format.

Code:

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

public class DigitalClock extends JFrame {

private JLabel timeLabel;

public DigitalClock() {
// Setting up the frame
setTitle("Digital Clock");
setSize(300, 150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);

// Adding a label to display time
timeLabel = new JLabel();
timeLabel.setFont(new Font("Arial", Font.PLAIN, 48));
timeLabel.setHorizontalAlignment(SwingConstants.CENTER);

// Adding the label to the frame
add(timeLabel);

// Timer to update the clock every second
Timer timer = new Timer(1000, e -> updateClock());
timer.start();
}

// Method to update the clock every second
private void updateClock() {
// Get the current time
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String currentTime = sdf.format(new Date());

// Update the label with the new time
timeLabel.setText(currentTime);
}

public static void main(String[] args) {
// Create and show the digital clock
SwingUtilities.invokeLater(() -> {
DigitalClock clock = new DigitalClock();
clock.setVisible(true);
});
}
}


Explanation:

  • JFrame: The main window of the clock, where the time is displayed.
  • JLabel: A label component is used to show the current time. The text is updated every second.
  • SimpleDateFormat: Used to format the current time in HH:mm:ss format (hours:minutes:seconds).
  • Timer: The javax.swing.Timer object is set to call the updateClock() method every 1000 milliseconds (1 second).
  • Font: The time is displayed in a large font (48 size), which can be adjusted for your preference.

How to run the code:

  1. Copy and paste the code into a .java file (for example, DigitalClock.java).
  2. Compile and run the file using a Java IDE or command line:
    • To compile: javac DigitalClock.java
    • To run: java DigitalClock

Once you run the program, it will show a window with the current time updating every second.

Aaradhya, an M.Tech student, is deeply engaged in research, striving to push the boundaries of knowledge and innovation in their field. With a strong foundation in their discipline, Aaradhya conducts experiments, analyzes data, and collaborates with peers to develop new theories and solutions. Their affiliation with "4achievres" underscores their commitment to academic excellence and provides access to resources and mentorship, further enhancing their research experience. Aaradhya's dedication to advancing knowledge and making meaningful contributions exemplifies their passion for learning and their potential to drive positive change in their field and beyond.

Explore the latest job openings

Looking for more job opportunities? Look no further! Our platform offers a diverse array of job listings across various industries, from technology to healthcare, marketing to finance. Whether you're a seasoned professional or just starting your career journey, you'll find exciting opportunities that match your skills and interests. Explore our platform today and take the next step towards your dream job!

See All Jobs

Explore the latest blogs

Looking for insightful and engaging blogs packed with related information? Your search ends here! Dive into our collection of blogs covering a wide range of topics, from technology trends to lifestyle tips, finance advice to health hacks. Whether you're seeking expert advice, industry insights, or just some inspiration, our blog platform has something for everyone. Explore now and enrich your knowledge with our informative content!

See All Bogs

Enrolling in a course at 4Achievers will give you access to a community of 4,000+ other students.

Email

Our friendly team is here to help.
Info@4achievers.com

Phone

We assist You : Monday - Sunday (24*7)
+91-801080-5667
Register for Free Demo
By clicking the button above, you agree to our Terms of Use and Privacy Policy. We do not share this information.

Whatsapp

Call