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?
Milind Kulkarni
Ranch Hand
Joined: Jun 01, 2000
Posts: 146
posted
0
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
Ajith Kallambella
Sheriff
Joined: Mar 17, 2000
Posts: 5781
posted
0
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
Open Group Certified Master IT Architect.
Sun Certified Architect(SCEA).
Serge Plourde
Ranch Hand
Joined: Jun 23, 2000
Posts: 140
posted
0
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
posted
0
Thanks Ajith. By the way, can I use javax.swing.Timer from within a GUI that uses only the regular AWT?