Results 1 to 4 of 4
Thread: Problem with timer in java
- 07-16-2007, 05:08 PM #1
Member
- Join Date
- Jul 2007
- Posts
- 26
- Rep Power
- 0
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:
And seems to work fine.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 ... ... ...
Why is the first second displayed as being 9 hours long? How do I correct this issue?
Thanks.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))); } });
- 07-25-2007, 11:21 PM #2
Member
- Join Date
- Jul 2007
- Posts
- 55
- Rep Power
- 0
If your using EJB's, this would be perfect use of an interceptor.
- 07-25-2007, 11:21 PM #3
Member
- Join Date
- Jul 2007
- Posts
- 55
- Rep Power
- 0
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)");
}
}
}
- 07-26-2007, 10:18 AM #4
Member
- Join Date
- Jul 2007
- Location
- England, Bath
- Posts
- 47
- Rep Power
- 0
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:
That should sort you out matey.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())); } });
Similar Threads
-
How to cancel an individual timer in spite of canceling whole timer
By Java Tip in forum java.utilReplies: 0Last Post: 04-04-2008, 02:46 PM -
Timer usage in java program
By sandeeprao.techno in forum New To JavaReplies: 5Last Post: 01-24-2008, 07:16 PM -
making a count down timer using java
By saytri in forum New To JavaReplies: 3Last Post: 12-29-2007, 09:49 PM -
Help with timer in java
By barney in forum Advanced JavaReplies: 1Last Post: 08-01-2007, 10:24 AM -
problem with timer
By Marcus in forum Advanced JavaReplies: 2Last Post: 07-01-2007, 05:13 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks