Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Help getting current time to start

  1. #1
    Member
    Join Date
    Sep 2013
    Posts
    36
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Help getting current time to start

    My issue is I can't get the current time to start counting by seconds for example 22:17:51 - 22:17:52 - 22:17:53. Any help will be greatly appreciated and here is my code so far. I can get the time to display just not start.

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.Timer;
    import java.util.Date;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
     
    public class TimeCounter extends JFrame
    {  //instance variables
       JLabel counterLabel;
       int count = 1;
       //inner class to listen to Timer
       DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss ");
       //get current date time with Date()
    	Date date = new Date();
     
       class TimerListener implements ActionListener
       {   
          public void actionPerformed(ActionEvent event)
          {
           counterLabel.setText(dateFormat.format(date));        
          }
       }
     
       //constructor for SecondCounter and JFrame
       public TimeCounter()
       {
          setSize(200, 100);
          counterLabel = new JLabel("Date: "+date);
          add(counterLabel);
          ActionListener listener = new TimerListener();
          // Milliseconds between timer ticks
          final int DELAY = 1000;       
          Timer t = new Timer(DELAY, listener);
          t.start();   //start the time   
       }
    }

    then the driver

     
    import javax.swing.JFrame;
     
    /** 	 	 	 	 	 	
       This program moves the rectangle.
    */
    public class TimeCounterViewer
    {
       public static void main(String[] args)
       {
          JFrame frame = new TimeCounter();
          frame.setTitle("Time Counter");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setVisible(true);
       }
    }

    Thanks for taking the time to look at my code and help.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Help getting current time to start

    If you are expecting to see a display that keeps ticking over like a clock then you will need to continually update your GUI to display the new time each second.
    Improving the world one idiot at a time!

  3. #3
    Member
    Join Date
    Sep 2013
    Posts
    36
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Help getting current time to start

    How do I continually update the GUI exactly ?? That is exactly what I'm trying to do.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Help getting current time to start

    Google for a java tutorial.
    Improving the world one idiot at a time!

  5. #5
    Member
    Join Date
    Sep 2013
    Posts
    68
    My Mood
    Confused
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default Re: Help getting current time to start

    you should use repaint() method that change the value of counterLabel.

  6. #6
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: Help getting current time to start

    int delay = 1000; //milliseconds
    ActionListener taskPerformer = new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
    //...Perform a task...
    }
    };
    new Timer(delay, taskPerformer).start();
    ... from api

Similar Threads

  1. Using Date() to get Start Time and Finish Time of a copyFiles method
    By dalythe in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: June 17th, 2013, 09:50 PM
  2. Applet viewer window is diplaying in fron of the current window every time
    By jsreddy99 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 24th, 2013, 07:16 AM
  3. Replies: 1
    Last Post: July 22nd, 2011, 07:08 AM
  4. How to Get the current date and time
    By JavaPF in forum Java Programming Tutorials
    Replies: 2
    Last Post: December 2nd, 2008, 01:55 PM
  5. How to Get the current date and time
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 2
    Last Post: December 2nd, 2008, 01:55 PM