Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-16-2007, 07:08 PM
Member
 
Join Date: Jul 2007
Posts: 26
paul is on a distinguished road
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:
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?

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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-26-2007, 01:21 AM
Member
 
Join Date: Jul 2007
Posts: 55
Seemster is on a distinguished road
If your using EJB's, this would be perfect use of an interceptor.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-26-2007, 01:21 AM
Member
 
Join Date: Jul 2007
Posts: 55
Seemster is on a distinguished road
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)");
}

}
}
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-26-2007, 12:18 PM
Member
 
Join Date: Jul 2007
Location: England, Bath
Posts: 47
shanePreater is on a distinguished road
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:
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.
__________________
Shane Preater -
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to cancel an individual timer in spite of canceling whole timer Java Tip java.util 0 04-04-2008 04:46 PM
Timer usage in java program sandeeprao.techno New To Java 5 01-24-2008 09:16 PM
making a count down timer using java saytri New To Java 3 12-29-2007 11:49 PM
Help with timer in java barney Advanced Java 1 08-01-2007 12:24 PM
problem with timer Marcus Advanced Java 2 07-01-2007 07:13 AM


All times are GMT +3. The time now is 04:28 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org