Web Analytics Made Easy -
StatCounter Java - Timer - with stop watch like display - CodingForum

Announcement

Collapse
No announcement yet.

Java - Timer - with stop watch like display

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Java - Timer - with stop watch like display

    I would like to time (and display) how long it takes my program to preform a certain task...

    Actually, I have already accomplished this but with one issue:

    my timer is displayed in a label 00:00:00

    when I start the timer the first second appears like this: 09:00:00
    then: 09:00:01
    then: 09:00:02
    then: 09:00:03
    ...
    ...
    ...

    and seems to work fine.

    Why is the first second displayed as being 9 hours long? How do I correct this issue?

    here is the code for my timer:
    Code:
        long count = 0;
        private final SimpleDateFormat TIME = new SimpleDateFormat("hh:mm:ss");
        Timer stopWatch = new Timer(1000, new ActionListener(){
            public void actionPerformed(ActionEvent event){
                timerDisplay.setText(TIME.format(new Date(count++ * 1000)));
            }
        });
    Thanks,

    Yak

  • #2
    I believe it's your date

    Code:
    new Date(count++ * 1000)
    according to the api:

    public Date(long date)

    Constructs a Date object using the given milliseconds time value. If the given milliseconds value contains time information, the driver will set the time components to the time in the default time zone (the time zone of the Java virtual machine running the application) that corresponds to zero GMT.

    Parameters:
    date - milliseconds since January 1, 1970, 00:00:00 GMT not to exceed the milliseconds representation for the year 8099. A negative number indicates the number of milliseconds before January 1, 1970, 00:00:00 GMT.
    Specifically that Parameters part. I think that may be causing an issue.

    To be honest I didn't believe it either but I created a short program to print out Date to see.

    Code:
    import java.util.*;
    import java.text.*;
    
    public class DateTest {
    
    	public static void main(String args[])
    	{
    		SimpleDateFormat TIME = new SimpleDateFormat("hh:mm:ss");
    		Date d = new Date(1000);
    		System.out.println(TIME.format(d));
    		System.out.println(d);
    	}
    	
    }
    Output:

    Code:
    07:00:01
    Wed Dec 31 19:00:01 EST 1969
    So it looks like the method you are trying to do it wont work specifically.
    "To iterate is human, to recurse divine." -L. Peter Deutsch

    Comment


    • #3
      Aradon, thanks for the reply.

      I originally thought it was:
      Code:
      new Date(count++ * 1000)
      as well...

      but I get the same results if I remove it.
      i.e.
      Code:
      timerDisplay.setText(TIME.format(count++ * 1000))
      the first second still registers as 9 hours...then works fine.

      not much more code to look through to find the issue. Currently looking into "SimpleDateFormat" as the possible root of the problem.

      so here is the code now: (not much different, no Date)
      Code:
          long count = 0;
          private final SimpleDateFormat TIME = new SimpleDateFormat("hh:mm:ss");
          Timer stopWatch = new Timer(1000, new ActionListener(){
              public void actionPerformed(ActionEvent event){
                  timerDisplay.setText(TIME.format(count++ * 1000));
              }
          });
      So the question still stands; why does the first second come up as 9 hours?

      Any help would be greatly appreciated.

      Thanks,

      Yak

      Comment


      • #4
        My Intuition states that putting in an int / long like that the format probably converts it into a date.

        Here is what I suggest. Date has basically become a deprecated class with little functionality left. So what should you use? GregorianCalendar.

        Since you aren't using milliseconds, you can construct a GregorianCalendar as such:

        GregorianCalendar(int year, int month, int dayOfMonth, int hourOfDay, int minute, int second)
        Constructs a GregorianCalendar with the given date and time set for the default time zone with the default locale.
        So to give you an example:

        Code:
        import java.util.*;
        
        public class DateTest {
        
        	public static void main(String args[])
        	{
        		GregorianCalendar gc = new GregorianCalendar(0,0,0,0,0,1);
        		System.out.println(gc.get(Calendar.HOUR) +" : " + gc.get(Calendar.MINUTE) + " : " + gc.get(Calendar.SECOND));
        		gc = new GregorianCalendar(0,0,0,0,0,70);
        		System.out.println(gc.get(Calendar.HOUR) +" : " + gc.get(Calendar.MINUTE) + " : " + gc.get(Calendar.SECOND));
        	}
        	
        }
        Which gives you output of:

        Code:
        0 : 0 : 1
        0 : 1 : 10
        All of the reasoning behind using the get and the constructor as well as many other examples are found in the api:

        GregorianCalendar

        Hope this helps!
        Last edited by Aradon; Oct 12, 2006, 07:14 AM.
        "To iterate is human, to recurse divine." -L. Peter Deutsch

        Comment


        • #5
          Specifically that Parameters part. I think that may be causing an issue.

          To be honest I didn't believe it either but I created a short program to print out Date to see.


          Code:

          import java.util.*;
          import java.text.*;

          public class DateTest {

          public static void main(String args[])
          {
          SimpleDateFormat TIME = new SimpleDateFormat("hh:mm:ss");
          Date d = new Date(1000);
          System.out.println(TIME.format(d));
          System.out.println(d);
          }

          }Output:


          Code:
          07:00:01
          Wed Dec 31 19:00:01 EST 1969So it looks like the method you are trying to do it wont work specifically.
          Yes sir, if you change your code to:
          Code:
          import java.util.*;
          import java.text.*;
          
          public class DateTest {
          
          	public static void main(String args[])
          	{
          		SimpleDateFormat TIME = new SimpleDateFormat("hh:mm:ss");
          		Date d = new Date(0,0,0);
          		System.out.println(TIME.format(d));
          		System.out.println(d);
          	}
          	
          }
          You get the following output:
          Code:
           ----jGRASP exec: java DateTest
          
          12:00:00
          Sun Dec 31 00:00:00 EST 1899
          
           ----jGRASP: operation complete.
          Not sure how it would help Yaki, but I post it just in case.

          Comment


          • #6
            Originally posted by daniel_g View Post
            Yes sir, if you change your code to:
            Code:
            import java.util.*;
            import java.text.*;
            
            public class DateTest {
            
            	public static void main(String args[])
            	{
            		SimpleDateFormat TIME = new SimpleDateFormat("hh:mm:ss");
            		Date d = new Date(0,0,0);
            		System.out.println(TIME.format(d));
            		System.out.println(d);
            	}
            	
            }
            You get the following output:
            Code:
             ----jGRASP exec: java DateTest
            
            12:00:00
            Sun Dec 31 00:00:00 EST 1899
            
             ----jGRASP: operation complete.
            Not sure how it would help Yaki, but I post it just in case.
            Well yes, but that is a deprecated method in the Date Class :P
            "To iterate is human, to recurse divine." -L. Peter Deutsch

            Comment


            • #7
              Thanks for the suggestions Aradon and daniel_g,

              The whole GregorianCalender solution appears to work, but seems a little cumbersome to add as my little stop watch.

              I have found a solution, I had to cheat a little, but it works...

              Basically I have chosen a random date and time in Milliseconds. Since I don't care about the year, month, day, etc. it doesn't matter what thier values are as long as the time initially appears 00:00:00. (the 24th hour of some random day)

              I will make this a final variable that will essentially be my start time.
              Code:
                  long count = 80492400;
              then I just increment it using the code I provided earlier.

              Let me know what you think?

              Yak

              Comment


              • #8
                Originally posted by Yakisoba View Post
                Thanks for the suggestions Aradon and daniel_g,

                The whole GregorianCalender solution appears to work, but seems a little cumbersome to add as my little stop watch.

                I have found a solution, I had to cheat a little, but it works...

                Basically I have chosen a random date and time in Milliseconds. Since I don't care about the year, month, day, etc. it doesn't matter what thier values are as long as the time initially appears 00:00:00. (the 24th hour of some random day)

                I will make this a final variable that will essentially be my start time.
                Code:
                    long count = 80492400;
                then I just increment it using the code I provided earlier.

                Let me know what you think?

                Yak
                If it works, it works. In reality this is probably a faster solution then creating the Calendar. However, I'm unsure how of that number will react on different machines.

                For example, when both you and I ran our seperate tests on Date you got a time that was 09 in hours while mine was at 07.

                Since the time is based off of epoch January 1, 1970, 00:00:00 GMT

                Now the reason for this is probably a time zone issue of some type, me being in the Eastern US and you being somewhere else.

                In any case I did a test:

                Code:
                import java.util.*;
                import java.text.*;
                
                public class DateTest {
                
                	public static void main(String args[])
                	{
                		SimpleDateFormat TIME = new SimpleDateFormat("hh:mm:ss");
                		Date d = new Date(80492400);  // Long time given
                		System.out.println(d);
                		d = new Date(80492400 + (1000)); // add one second
                		System.out.println(d);
                	}
                	
                }
                Now I _could_ be misreading the way you are using long (which at 4:30 am is a good possibility) However I get the following results

                Code:
                Thu Jan 01 17:21:32 EST 1970
                Thu Jan 01 17:21:33 EST 1970
                As you can tell, I don't get 0:0:0 for that time in miliseconds.

                Is this how you are using the code or am I missing something?

                Now granted if this piece of code isn't going to any important application then it may be best to continue with the way you've done it. However if you ever plan to expand upon it or pass it to someone, it might be best to go with a more robust method.
                "To iterate is human, to recurse divine." -L. Peter Deutsch

                Comment


                • #9
                  Aradon,

                  Here is the code I came up with...(I don't use "Date", that could be the difference you witnessed. To make sure, if you have a chance, can you give this code a test?).
                  Code:
                      private static final long START_TIME = 80492401;
                  
                      long count = START_TIME;
                      private final SimpleDateFormat TIME = new SimpleDateFormat("HH:mm:ss");
                      Timer stopWatch = new Timer(1000, new ActionListener(){
                          public void actionPerformed(ActionEvent event){
                              timerDisplay.setText(TIME.format(count++ * 1000));
                          }
                      });
                  Let me know if this works out for you?

                  Thanks,

                  Yak

                  Comment


                  • #10
                    With this code:

                    Code:
                    import java.util.*;
                    import java.text.*;
                    
                    public class DateTest {
                    	
                        private static final long START_TIME = 80492401;
                        
                    	public static void main(String args[])
                    	{
                    		long count = START_TIME;
                    		SimpleDateFormat TIME = new SimpleDateFormat("HH:mm:ss");
                    		for(int i = 0; i < 10; i++)
                    		  System.out.println(TIME.format(count++ * 1000));
                    	}
                    	
                    }
                    I get the following output:

                    11:00:01
                    11:00:02
                    11:00:03
                    11:00:04
                    11:00:05
                    11:00:06
                    11:00:07
                    11:00:08
                    11:00:09
                    11:00:10
                    I basically used your code and did a slight copy and paste..
                    "To iterate is human, to recurse divine." -L. Peter Deutsch

                    Comment


                    • #11
                      I replied to this a while ago, but I guess it didn't post...

                      Anyway, thanks a lot for looking into and testing my code. It appears that my random start time is specific to my current time zone (system time zone settings).

                      I was able to replicate your output by changing the Time zone.

                      I'll have to look into your previous suggestion and maybe create an instance of Calendar to get a more robust solution.

                      Thanks again,

                      Yak

                      Comment

                      Working...
                      X