Pausing Without "Thread.sleep(int s)"

Hi!

I am trying to create a clock that is hours:minutes, except that the minutes increment every second in real time and the hours update ever minute in real time (still looping around at 24, though), without pausing the rest of my program.

I don't want to make the class implement Thread if at all possible.

publicclass Timer

{

privatestatic String theTime ="06:00";

privatestaticint lastSecond = (int)System.nanoTime() * 1000000000;

publicstatic String getTime()

{

int currentTime = (int)(System.nanoTime() * 1000000000);

if( currentTime != lastSecond )// <-- NOT WORKING; this seems to be true each time the method is called, not every second.

{

String minute = theTime.substring(3);

String hour = theTime.substring(0, 2);

int min = Integer.parseInt(minute);

min++;

if(min <= 9)

minute ="0" + min +"";

else

minute = min +"";

if(minute.equals("60"))

{

int h = Integer.parseInt(hour);

h++;

if(h <= 9)

hour ="0" + h +"";

else

hour = h +"";

minute ="00";

}

if(hour.equals("25"))

{

hour ="00";

}

lastSecond = currentTime;

theTime = hour +":" + minute;

}

return theTime;

}

publicstaticvoid tester()

{

while(true)

System.out.println(getTime());

}

}

[2977 byte] By [da_java_mana] at [2007-11-27 9:40:11]
# 1

There are two Timer classes in the API, one in java.util and one in javax.swing. These objects can easilly be set up to invoke your code every second. Of course they use threads, but you don't have to worry about them.

Incidentally you don't make classes implement thread, you make them implement Runnable and create a Thread object pointing to them.

malcolmmca at 2007-7-12 23:16:52 > top of Java-index,Java Essentials,Java Programming...
# 2
> Incidentally you don't make classes implement thread,> you make them implement Runnable and create a Thread> object pointing to them.That's what I meant to say :).Thanks for the help!
da_java_mana at 2007-7-12 23:16:52 > top of Java-index,Java Essentials,Java Programming...