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());
}
}

