User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 456,507 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,666 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 4331 | Replies: 8
Reply
Join Date: Aug 2004
Posts: 11
Reputation: i_me_roo is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
i_me_roo i_me_roo is offline Offline
Newbie Poster

displaying time

  #1  
Aug 9th, 2004
Hi,

Is there a way of timing how long it takes between a piece of code executing to when it finishes and then displaying this time? I would like to display a clock which begins timing when a program starts running and stops when it finishes.

Thanks
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2004
Location: Pakistan
Posts: 1,673
Reputation: nanosani is an unknown quantity at this point 
Rep Power: 8
Solved Threads: 50
Colleague
nanosani's Avatar
nanosani nanosani is offline Offline
Unauthenticated Liar

Re: displaying time

  #2  
Aug 9th, 2004
just add a timer at the start of the program execution .... there are two types of timers .... one for gui and one is general
general:
java.util.Timer
Timer t = new Timer();

gui:
javax.swing.*
Timer t = new Timer(ActionListener, delay);

Read more about the timers in the java API.
Reply With Quote  
Join Date: Aug 2004
Location: Hanover
Posts: 152
Reputation: cosi is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 1
cosi's Avatar
cosi cosi is offline Offline
Junior Poster

Re: displaying time

  #3  
Aug 10th, 2004
An alternative solution, I've used to as a cheap profiling trick in big programs:

long startTime = System.currentTimeMillis(); // time at start in millisecs

//
//code
//

long stopTime = System.currentTimeMillis(); // time at end

// time elapsed is stopTime-startTime

See this link for details.
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html#currentTimeMillis()
Reply With Quote  
Join Date: Aug 2004
Posts: 11
Reputation: i_me_roo is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
i_me_roo i_me_roo is offline Offline
Newbie Poster

Re: displaying time

  #4  
Aug 11th, 2004
Thanx a lot for ur suggestions, been a great help!
Reply With Quote  
Join Date: Mar 2008
Posts: 3
Reputation: Aldehyde81 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Aldehyde81 Aldehyde81 is offline Offline
Newbie Poster

Re: displaying time

  #5  
Mar 3rd, 2008
Originally Posted by cosi View Post
An alternative solution, I've used to as a cheap profiling trick in big programs:

long startTime = System.currentTimeMillis(); // time at start in millisecs

//
//code
//

long stopTime = System.currentTimeMillis(); // time at end

// time elapsed is stopTime-startTime

See this link for details.
http://java.sun.com/j2se/1.4.2/docs/...ntTimeMillis()



it display in this format
1204531604270
How to display in time format?
Reply With Quote  
Join Date: Dec 2007
Location: Greece
Posts: 658
Reputation: javaAddict has a spectacular aura about javaAddict has a spectacular aura about 
Rep Power: 3
Solved Threads: 76
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Practically a Master Poster

Re: displaying time

  #6  
Mar 3rd, 2008
What you see is in milliseconds. When you get the difference, multiply by 1000 and you will have how many seconds have passed
I AM the 12th CYLON
Reply With Quote  
Join Date: Mar 2008
Posts: 3
Reputation: Aldehyde81 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Aldehyde81 Aldehyde81 is offline Offline
Newbie Poster

Re: displaying time

  #7  
Mar 3rd, 2008
I mean, I want to display it in hh:mm:ss
Can anyone suggest?
Reply With Quote  
Join Date: Dec 2007
Location: Greece
Posts: 658
Reputation: javaAddict has a spectacular aura about javaAddict has a spectacular aura about 
Rep Power: 3
Solved Threads: 76
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Practically a Master Poster

Re: displaying time

  #8  
Mar 3rd, 2008
You have a value in milliseconds. You can either use SimpleDateFormat, or do it yourself using the following rules:
1 sec = 1000 millisec
1 min = 60 sec
1 hour = 60 min

And sorry about my last post, I made a very big mistake: You must divide by 1000 to get the seconds
I AM the 12th CYLON
Reply With Quote  
Join Date: Mar 2008
Posts: 3
Reputation: Aldehyde81 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Aldehyde81 Aldehyde81 is offline Offline
Newbie Poster

Re: displaying time

  #9  
Mar 3rd, 2008
Thanks..
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 3:39 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC