PDA

Click to See Complete Forum and Search --> : Formatting Time?


Dillinger4
Jul 16th, 2003, 03:25 PM
Im trying to create a timer that executes in a seperate thread but i have no clue how to go about formatting the time in h:m:s format. I tried using the java.util.Timer and java.util.TimerTask classes but they are a pain to use so i decided just to use an infinite loop. Thanks

spawnTimerThread();


public void spawnTimerThread(){
DateFormat df = DateFormat.getTimeInstance();
// how do i format to h:m:s

Thread timer = new Thread();
t.setDemon(true); // let the vm exit;
for(;;){
timer.sleep(1000);
// how would i increment the fields
}
}

Dillinger4
Jul 16th, 2003, 03:40 PM
I think i can use the SimpleDateFormat class to create the h:m:s pattern but im still not sure how to increment the values of those fields. Ill have to look into this when i get back from work tonight. If anyone has any ideas please feel free. :D

SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");

Dillinger4
Jul 17th, 2003, 02:52 AM
This seems to do what i want but the only thing i don't like is the use of the Strings. It would be more efficent if i could use one object and just set the time values. I noticed that the java.util.Date class has these methods but all of them have been deprecated. ie public void setSeconds(int seconds), public void setMinutes(int minutes)

Also since the timer is running as a seperate thread of execution my GUI keeps locking up. Any suggestions?

public class X{
public static void main(String args[]){
X x = new X();
x.spawnTimerThread();
}
public void spawnTimerThread(){

int seconds = 0;
int minutes = 0;
int hours = 0;

for(;;){
try{
Thread.sleep(1000);
seconds = ++seconds;
if(seconds == 60){
minutes = ++minutes;
seconds = 0;
}
if(minutes == 60){
hours = ++hours;
minutes = 0;
}
System.out.println(hours+":"+ minutes+":"+ seconds);
}catch(InterruptedException e){;}
}
}
}

CornedBee
Jul 17th, 2003, 03:01 AM
Use Calendar.

Dillinger4
Jul 17th, 2003, 03:05 AM
Yeah thats what i used before but for some reason i couldnt get anything to work right. :D Ill leave it like it is for now just so i can try and fix the repainting problem.

Dillinger4
Jul 25th, 2003, 12:14 AM
This seemed to kill the GUI lockup.

public void actionPerformed(ActionEvent ae){
Thread timer = new Thread(this);
timer.start();

public void run(){
int seconds = 0;
int minutes = 0;
int hours = 0;
for(;;){
try{
Thread.sleep(1000);
seconds = ++seconds;
if(seconds == 60){
minutes = ++minutes;
seconds = 0;
}
if(minutes == 60){
hours = ++hours;
minutes = 0;
}
tt.timerscreen.setText(new String(hours+":"+ minutes+":"+ seconds));
}catch(InterruptedException e){;}
}
}
}

CornedBee
Jul 25th, 2003, 05:00 AM
Not very exact though. Sleep(1000) is not guaranteed to sleep exactly 1000 miliseconds, it's only a minimum. Plus, your code takes time to execute too.

Dillinger4
Jul 25th, 2003, 12:38 PM
Don't worry. It's not a time critical application. Now if i was running this code on the space shuttle i doubt i would feel safe. :D

CornedBee
Jul 27th, 2003, 04:14 PM
:p

But don't let the app run a year non-stop and then try to judge exact times by it. It will be off several minutes by then.