Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi, I'm trying to create a timer

Status
Not open for further replies.

groovygarden

Programmer
Aug 23, 2001
63
Hi,

I'm trying to create a timer that will count up from 0 to measure and display the length of time a user has been active - updating in seconds. This is an extract from my code:

Code:
public void actionPerformed(ActionEvent event)
  {

Date today  = new Date();;
String result;
SimpleDateFormat formatter = new SimpleDateFormat("mm:ss");

today.setMinutes(00);
today.setSeconds(00);

result = formatter.format(today);

System.out.println("Result: " + result);

If I set minutes and seconds to 00, all that happens is that the system prints out 00:00 at one second intervals. If I don't set them, it prints the correct current time updating at one second intervals.

What am I doing wrong?

Thanks
 
Hi

The instruction new getDate() creates a Date instance that contains the current date.

You have to save the starting date when your program start. For example, in the constructor:
this.startDate = new Date();

Then , in your event, something like this:
Date today = new Date();
today.setTime(today.getTime() - startDate.getTime());
// ...

Thomas A free GUI for your database:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top