Results 1 to 9 of 9

Thread: Formatting Time?

Hybrid View

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Formatting Time?

    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
    Code:
    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
      }
    }

  2. #2

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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.
    Code:
    SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");

  3. #3

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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?
    Code:
    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){;}
          }
         }
        }

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Use Calendar.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Yeah thats what i used before but for some reason i couldnt get anything to work right. Ill leave it like it is for now just so i can try and fix the repainting problem.

  6. #6

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    This seemed to kill the GUI lockup.
    Code:
    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){;}
          }
         }
        }

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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.

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594


    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.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width