Note - Double Click to Copy Code Contact Us!

Digital Clock in Java

Tech Doubility
Digital Clock in Java

HTML
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Date;
  5.  
  6. public class DigitalClockApp extends JFrame {
  7. private JLabel timeLabel;
  8.  
  9. public DigitalClockApp() {
  10. setTitle("Digital Clock");
  11. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  12. setSize(200, 100);
  13. setLocationRelativeTo(null);
  14.  
  15. // Create the time label
  16. timeLabel = new JLabel();
  17. timeLabel.setFont(new Font("Arial", Font.BOLD, 24));
  18. timeLabel.setHorizontalAlignment(SwingConstants.CENTER);
  19. updateTime();
  20.  
  21. // Set the time label as the content pane
  22. setContentPane(timeLabel);
  23.  
  24. // Start a timer to update the time every second
  25. Timer timer = new Timer(1000, e -> updateTime());
  26. timer.start();
  27. }
  28.  
  29. private void updateTime() {
  30. SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
  31. String time = dateFormat.format(new Date());
  32. timeLabel.setText(time);
  33. }
  34.  
  35. public static void main(String[] args) {
  36. SwingUtilities.invokeLater(() -> {
  37. DigitalClockApp app = new DigitalClockApp();
  38. app.setVisible(true);
  39. });
  40. }
  41. }
  42.  

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.