• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Get and print elapsed time

 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 hh:mm:ss.ms 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();
calendar.setTime(trialTime);
System.out.println("Started at: " +
calendar.get(calendar.HOUR_OF_DAY)+":"+
calendar.get(Calendar.MINUTE)+":"+
calendar.get(Calendar.SECOND)+"."+
calendar.get(Calendar.MILLISECOND));
//Make some time between start time and end time:
for (int i=1; i<200000000; i++){}
trialTime = new Date();
long end = trialTime.getTime();
calendar.setTime(trialTime);
System.out.println("Ended at: "+
calendar.get(calendar.HOUR_OF_DAY)+":"+
calendar.get(Calendar.MINUTE)+":"+
calendar.get(Calendar.SECOND)+"."+
calendar.get(Calendar.MILLISECOND));
long elapsed = end-start;
System.out.println(elapsed + " elapsed milliseconds.");
trialTime.setTime(elapsed);
calendar.setTime(trialTime);
int seconds = (int) elapsed / 1000;
int milliSeconds = (int) elapsed % 1000;
int minutes = seconds / 60;
seconds = seconds % 60;
int hours = minutes / 60;
minutes = minutes % 60;
System.out.println(
hours + " hours, " + minutes + " minutes, " +
seconds + " seconds, " + milliSeconds + " ms elapsed.");

//Why is the result of this statement wrong???
System.out.println("Elapsed time: "+
calendar.get(calendar.HOUR_OF_DAY)+":"+
calendar.get(Calendar.MINUTE)+":"+
calendar.get(Calendar.SECOND)+"."+
calendar.get(Calendar.MILLISECOND));
}
}
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?
 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sarge,
I got the similar results. And when I probed further I have got this information about setTime() method.
"setTime
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().
Regards,
Milind
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
Ajith
 
Serge Plourde
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ajith.
By the way, can I use javax.swing.Timer from within a GUI that
uses only the regular AWT?

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic