954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

displaying time

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

i_me_roo
Newbie Poster
11 posts since Aug 2004
Reputation Points: 10
Solved Threads: 0
 

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.

nanosani
Unauthenticated Liar
Team Colleague
1,830 posts since Jul 2004
Reputation Points: 45
Solved Threads: 56
 

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()

cosi
Junior Poster
153 posts since Aug 2004
Reputation Points: 17
Solved Threads: 1
 

Thanx a lot for ur suggestions, been a great help! :D

i_me_roo
Newbie Poster
11 posts since Aug 2004
Reputation Points: 10
Solved Threads: 0
 

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()


it display in this format
1204531604270
How to display in time format?

Aldehyde81
Newbie Poster
3 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

What you see is in milliseconds. When you get the difference, multiply by 1000 and you will have how many seconds have passed

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

I mean, I want to display it in hh:mm:ss
Can anyone suggest?

Aldehyde81
Newbie Poster
3 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

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

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

Thanks..

Aldehyde81
Newbie Poster
3 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You