Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do I refresh the system time

 
Nikhilesh Gandhi
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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]
 
Eddie Vanda
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic