Results 1 to 4 of 4
  1. #1
    paul is offline Member
    Join Date
    Jul 2007
    Posts
    26
    Rep Power
    0

    Default Problem with timer in java

    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:
    Java Code:
    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?

    Java 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.

  2. #2
    Seemster is offline Member
    Join Date
    Jul 2007
    Posts
    55
    Rep Power
    0

    Default

    If your using EJB's, this would be perfect use of an interceptor.

  3. #3
    Seemster is offline Member
    Join Date
    Jul 2007
    Posts
    55
    Rep Power
    0

    Default

    public class TestProfiler {
    @AroundInvoke
    public Object profile(InvocationContext invocation) throws Exception {
    long startTime = System.currentTimeMillis();
    try {
    return invocation.proceed();
    } finally {
    long endTime = System.currentTimeMillis( ) - startTime;
    System.out.println("Method " + invocation.getMethod( )
    + " took " + endTime + " (ms)");
    }

    }
    }

  4. #4
    shanePreater is offline Member
    Join Date
    Jul 2007
    Location
    England, Bath
    Posts
    47
    Rep Power
    0

    Default

    This is because you are creating you Date object wrong.

    Basically the Date(long time) constructor is expecting the time in millis which is the number of milliseconds since dec 31 1970 (iirc) what you actually want to do is:
    Java Code:
    new ActionListener(){
            public void actionPerformed(ActionEvent event){
                Calendar calendar = Calendar.getInstance();
    	    calendar.set(Calendar.SECOND, count * 1000);
                timerDisplay.setText(TIME.format(calendar.getTime()));
            }
        });
    That should sort you out matey.

Similar Threads

  1. Replies: 0
    Last Post: 04-04-2008, 02:46 PM
  2. Timer usage in java program
    By sandeeprao.techno in forum New To Java
    Replies: 5
    Last Post: 01-24-2008, 07:16 PM
  3. making a count down timer using java
    By saytri in forum New To Java
    Replies: 3
    Last Post: 12-29-2007, 09:49 PM
  4. Help with timer in java
    By barney in forum Advanced Java
    Replies: 1
    Last Post: 08-01-2007, 10:24 AM
  5. problem with timer
    By Marcus in forum Advanced Java
    Replies: 2
    Last Post: 07-01-2007, 05:13 AM

Posting Permissions

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