add

"Hi,

I have this program that displays a random time. (updateTime() method does that)

What I want to do now is that when the user press a Button, this

button adds an hour to that random time.

I'm calling updateTime2() method to do this task, but what is doing right now

is: adding an hour to the system time(not the random).

what can I do to solve this?

thanks for any response...

publicvoid updateTime(){

Random randomizer =new Random();

int randomInt = randomizer.nextInt();

Date now =new Date(System.currentTimeMillis() +randomInt);

String time = now.toString().substring(11,23);

setText(time);

}

publicvoid updateTime2(){

int anHour = 1000*60*60;///////////////new1000 to 2000

Date anHourLater =new Date(System.currentTimeMillis() +anHour);//////////new

String time = anHourLater.toString().substring(11,23);/////////new

setText(time);

}

[1481 byte] By [deroka] at [2007-11-27 2:19:10]
# 1
put the+randomIntback into the equation.Or better yet, save the previously determined time somewhere and simply add an hour to that.
masijade.a at 2007-7-12 2:19:11 > top of Java-index,Java Essentials,Java Programming...
# 2

I got that.

The button does add an hour to the time, but if I press it again it does nothing.

What can i do, so, everytime i press the button it will continue to add one hour to the time?

the code:

import java.awt.*;

import java.util.*;

import javax.swing.*;/////

import java.awt.event.*;

class TickerThread extends Thread {

ClockLabel c1;

public TickerThread(ClockLabel c) {

c1 = c;

}

public void run() {

while(true) {

try { sleep(1000);

}/////////// 2000 to 1000

catch (InterruptedException e) { break; }

//c1.updateTime();

}

}

}

public class ClockLabel extends Label{

Thread ticker;

public ClockLabel() {

super("Clock", Label.CENTER);

ticker = null;

}

public void startClock() {

//if (ticker == null) {

ticker = new TickerThread(this);

ticker.start();

//}

updateTime();

}

/////////////new

public void startClock2() {

//if (ticker2 == null) {

ticker = null;

ticker = new TickerThread(this);

ticker.start();

//}

updateTime2();

}

Date now = null;

public void updateTime() {

Random randomizer = new Random();

int randomInt = randomizer.nextInt();

now = new Date(System.currentTimeMillis() +randomInt);

String time = now.toString().substring(11,23);

setText(time);

}

public void updateTime2() {

int anHour = 1000*60*60; //////////new1000 to 2000

Date anHourLater = new Date(now.getTime() +anHour);//////changed////new

String time = anHourLater.toString().substring(11,23);//////new

setText(time);

}

//////MAIN******////////

public static void main(String [] args) {

Frame fr = new Frame("Clock test");

Label l1 = new Label("Clock");

///////new

Button b;

final ClockLabel c1 = new ClockLabel();/////////new

ActionListener al = new MyActionListener();

fr.add(b = new Button("Add hour"),

BorderLayout.EAST);

//new--

b.addActionListener(new AbstractAction() {

public void actionPerformed(ActionEvent e) {

c1.startClock2();

}

});

c1.startClock();//////////starts

deroka at 2007-7-12 2:19:11 > top of Java-index,Java Essentials,Java Programming...
# 3

Hmm...

new Date(now.getTime() +anHour);

here you get the current time and add an hour, so each time you do this, it will get the current time and add an hour to it. So for the most part, until another hour has actually passed, the only part of the time that will be different is the minutes...

you need to keep running total of hours added then add that to the current time...

totalAddedTime +=1000*60*60;

MaxxDmga at 2007-7-12 2:19:11 > top of Java-index,Java Essentials,Java Programming...
# 4
"you need to keep running total of hours added then add that to the current time...totalAddedTime +=1000*60*60;"didn't understand well this part.this goes in what method?
deroka at 2007-7-12 2:19:11 > top of Java-index,Java Essentials,Java Programming...