Saturday, April 20, 2013

Clock program

The Text Clock is a Java program to display a text-based clock to show the time progress. To get the current date and time, the  program uses Date class. To display  AM or PM along with the time, the Date object  needs to be formatted by using the SimpleDateFormat class. To make a real clock, the Timer class is used. The Timer class is able to do its task in a specified interval of time so that the progress of time can be shown.

Clock source code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Date;
import java.text.SimpleDateFormat;
class  Clock extends JFrame{
            JLabel lblshow;
1                      setDefaultCloseOperation(EXIT_ON_CLOSE);
2                      setTitle("Text Clock");
3                      setSize(100,70);
4                      lblshow=new JLabel("Current Time");
5                      lblshow.setForeground(Color.CYAN);
6                      Containter cont=getContentPane();                                
7                      cont.add(lblshow, BorderLayout.CENTER);
8                      cont.setBackground(Color.BLACK);
9                      setVisible(true);       

10        public void start(){
                        timer = new Timer(1000,new AListener());

11        class AListener implements ActionListener{
                        public void actionPerformed( ActionEvent e ) {
                                    Date dat=new Date();
                                    String ftime="HH:mm:ss a";
                                    SimpleDateFormat fd = new SimpleDateFormat(ftime);

12 public class TextClock{
            public static void main(String args[]){
                        Clock c=new Clock();


Code Explanation:
1 Close the program window when the close button is pushed.
2 Specify the title of the program window (Text Clock).
3 Specify the size of the program window to open (width=100, height=70).
4 Create the JLabel lblshow object. This label object is to display the time progress.
5 Specify the text color of the lblshow object (CYAN).
6 Create the Container cont object. The Container object is the workspace to place other components. In this program, only lblshow object is placed on the Container cont object.
7 Place the lblshow object on the Container cont.
8 Specify the background color of the Container cont (BLACK).
9 Make sure the program window is visible.
10 The start method has code to show time and its progress. The Timer timer object is created to show the time progress. The Timer class constructor is Timer(int delay, ActionListner). In this program, we want to show the time progress in each every second. So, the delay is  1000 milliseconds. The ActionListener has the actionPerformed method that needs to be implemented to enable the timer to do action in every second.
11 The AListener class implements the actionPerformed method of the ActionLister interface. When the timer performs its action in every second, the Date object is created and formatted by using the SimpleDateFormat class to show only the time (with AM or PM). The time progress is shown on the lblshow by using setText(string) method.
12 The TextClock class the main method to start showing the clock.


  1. I loved as much as you will receive carried out right here. The sketch is attractive, your authored material stylish. nonetheless, you command get bought an impatience over that you wish be delivering the following. unwell unquestionably come more formerly again as exactly the same nearly a lot often inside case you shield this hike. BrowSEO

  2. Hey! This post could not be written any better! Reading through this post reminds me of my good old room mate! He always kept talking about this. I will forward this article to him. Pretty sure he will have a good read. Thanks for sharing! good UX

  3. Excellent blog here! Also your web site loads up very fast! What web host are you using? Can I get your affiliate link to your host? I wish my site loaded up as quickly as yours lol. teeth whitening Singapore price

  4. I’m not that much of a internet reader to be honest but your blogs really nice, keep it up! I'll go ahead and bookmark your website to come back later on. how to make my business appear on google search

  5. Although SEO is an expensive affair, you should look forward to hiring a result driven advertising agency despite the price. You would be able to make the most of return on investment with a result-driven advertising agency in the long run.

  6. Very efficiently written post. It will be useful to everyone who utilizes it, including yours truly :). Keep doing what you are doing - for sure i will check out more posts.tentage company singapore

  7. Hey! This is kind of off topic but I need some advice from an established blog. Is it difficult to set up your own blog? I'm not very techincal but I can figure things out pretty quick. I'm thinking about making my own but I'm not sure where to begin. Do you have any tips or suggestions? Many thanks. architects Singapore

  8. I'm truly enjoying the design and layout of your blog. It's a very easy on the eyes which makes it much more pleasant for me to come here and visit more often. Did you hire out a developer to create your theme? Great work! hire purchase singapore