GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes How do I refresh the system time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How do I refresh the system time" Watch "How do I refresh the system time" New topic
Author

How do I refresh the system time

Nikhilesh Gandhi
Greenhorn

Joined: Feb 07, 2004
Posts: 8
Hi !
I want to display a digital clock in my application but i acn only get the current system time .
I does not refresh.
here is the code i am using it is a part of a longer prg but here is only what is relevant

public static class ttask extends TimerTask
{
public void run()
{
int time = calendar.get(Calendar.SECOND);
System.out.println(time);
}
}
public static void main(String[] args)
{
Timer myTimer = new Timer();
ttask myTask = new ttask();
myTimer.schedule(myTask,1000,500);
InternalFrameDemo frame = new InternalFrameDemo();
try
{
Thread.sleep(5000);
}
catch(InterruptedException exc)
{}

}
}
Eddie Vanda
Ranch Hand

Joined: Mar 18, 2003
Posts: 281
Hi Nikhelesh,
I think that your main problems is that you don't update your calendar object.
The most efficient algorithm for getting seconds would be to get the milliscount from System, divide by 1000. and take modulo 60. I just newed the calendar object each time to get the current time.
I tried to simplify things a bit as follows:
[/code]
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class DigClock implements ActionListener {
static final JTextField jtf = new JTextField (5);
public void actionPerformed (ActionEvent ae) {
GregorianCalendar calendar = new GregorianCalendar ();
int time = calendar.get(Calendar.SECOND);
jtf.setText (String.valueOf (time));
}
public static void main(String[] args) {
DigClock myTask = new DigClock();
javax.swing.Timer myTimer = new javax.swing.Timer(400,myTask);
JFrame myFrame = new JFrame();
Container c = myFrame.getContentPane ();
c.add (jtf);
myFrame.pack ();
myFrame.setVisible (true);
myTimer.start ();
}
}
[/code]


The nice thing about Standards is that there are so many to choose from!
Eddie Vanda
Ranch Hand

Joined: Mar 18, 2003
Posts: 281
Originally posted by Eddie Vanda:
Hi Nikhilesh,
I think that your main problems is that you don't update your calendar object.
The most efficient algorithm for getting seconds would be to get the milliscount from System, divide by 1000. and take modulo 60. I just newed the calendar object each time to get the current time.
I tried to simplify things a bit as follows:
 
GeeCON Prague 2014
 
subject: How do I refresh the system time