This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Get and print elapsed time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Get and print elapsed time" Watch "Get and print elapsed time" New topic

Get and print elapsed time

Serge Plourde
Ranch Hand

Joined: Jun 23, 2000
Posts: 140
Hi, I'm making my first attempts to work in Java with time issues.
I'm just wondering if there is a "direct" method to print a long
elapsed time in milliseconds in a format. Also, see the
question at the end of the code sample. Thanks.
Here is a sample code:
import java.util.*;
public class TimeAndDate
public static void main(String[] args)
Calendar calendar = new GregorianCalendar();
Date trialTime = new Date();
long start = trialTime.getTime();
System.out.println("Started at: " +
//Make some time between start time and end time:
for (int i=1; i<200000000; i++){}
trialTime = new Date();
long end = trialTime.getTime();
System.out.println("Ended at: "+
long elapsed = end-start;
System.out.println(elapsed + " elapsed milliseconds.");
int seconds = (int) elapsed / 1000;
int milliSeconds = (int) elapsed % 1000;
int minutes = seconds / 60;
seconds = seconds % 60;
int hours = minutes / 60;
minutes = minutes % 60;
hours + " hours, " + minutes + " minutes, " +
seconds + " seconds, " + milliSeconds + " ms elapsed.");

//Why is the result of this statement wrong???
System.out.println("Elapsed time: "+
Here is a sample output:
Started at: 7:52:16.990
Ended at: 7:52:21.820
4830 elapsed milliseconds.
0 hours, 0 minutes, 4 seconds, 830 ms elapsed.
Elapsed time: 19:0:4.830
Question: Why is it showing 19 hours elapsed? Somehow it added 12 hours.
Thanks in advance folks!
P.s.: is there a "timer" class in the API?
Milind Kulkarni
Ranch Hand

Joined: Jun 01, 2000
Posts: 146
Hi Sarge,
I got the similar results. And when I probed further I have got this information about setTime() method.
public final void setTime(Date date)
Sets this Calendar's current time with the given Date.
Note: Calling setTime() with Date(Long.MAX_VALUE) or Date(Long.MIN_VALUE) may yield incorrect field values from get().
Ajith Kallambella

Joined: Mar 17, 2000
Posts: 5782
Java has a Timer class which notifies you after waiting for the specified amount of time. You will neeed to implement an ActionListener interface and pass it to the constructor of the Timer class. So far I have found this is the best way to implement a timer. Take a look at the Java API documentation for class javax.swing.Timer for more details.
You can also write your own little timer using Thread.sleep().
Good luck,

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Serge Plourde
Ranch Hand

Joined: Jun 23, 2000
Posts: 140
Thanks for your help.
So we have to make our own classes to do such basic time
processing as a timer?! And devising elapsed time.
Serge Plourde
Ranch Hand

Joined: Jun 23, 2000
Posts: 140
Thanks Ajith.
By the way, can I use javax.swing.Timer from within a GUI that
uses only the regular AWT?

With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Get and print elapsed time
jQuery in Action, 3rd edition