Link to home
Start Free TrialLog in
Avatar of AGE_Nicolls
AGE_Nicolls

asked on

How to calcutate duration using long milliseconds?

There doesn't seem to be a class in Java to calculates duration in hours, minutes, seconds, milliseconds, etc.

Take the curent code snippet...

//======================================================
long startTime = System.currentTimeMillis();
boolean done = false;

while(!done){
    // code to perform some operation...
    // that set's dome to true when complete
}

long endTime = System.currentTimeMillis();
long duration = endTime - startTime;
//======================================================


At this point, the duration variable holds the number of
milliseconds it took for the process to complete.  But what class can I put this into to convert it to days, hours, minutes, seconds, etc.?

My first thought was to put it into Date, and then use a GregorianCalendar to get the individial fields.  That didn't work becuase it uses GMT time zone as the basis, and I am in CST time zone - which means all of my times were off by 6 hours.  

I'm sure I can write a work around for this, but I would like to think that Sun has provided something to do this.  I would rather use their solution, than my own.

Anyone know of a class that does this?

AGE_Nicolls
ASKER CERTIFIED SOLUTION
Avatar of mzimmer74
mzimmer74

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mick Barry
Depends on exactly what you want to measure.
The easiest way is to simply do the calculations:

long seconds = duration/1000;
long minutes = seconds/60;
etc.

But this doesn't take into account any variations caused by things like daylight savings, leaps etc.
To take these sort of things into account, you will need to wrap your start and end Date's in Calendar objects and then calculate the difference using these.
Avatar of AGE_Nicolls
AGE_Nicolls

ASKER

mzimmer74:
Your solution did not work, but it did get me to the solution I was looking for - which is...

//======================================================
long startTime = System.currentTimeMillis();
boolean done = false;

while(!done){
   // code to perform some operation...
   // that set's dome to true when complete
}

long endTime = System.currentTimeMillis();

// !! Add the getRawOffset() of the current time zone !!
long duration = (endTime - startTime + TimeZone.getDefault().getRawOffset();

System.out.println(cal.get(cal.HOUR) + ":" + cal.get(cal.MINUTE) + ":" + cal.get(cal.SECOND) + "." + cal.get(cal.MILLISECOND));

//======================================================



objects:
Your solution works, but that is what I am doing already.  I have written a method that takes millis and parses out hours, minutes, seconds, etc.  I want to use existing classes to perform this calc, and not reinvent the wheel...



kotan:
That's a great article, but it basically tells me to do what 'objects' told me to do (which is write my own)  


Thanks - the points go to mzimmer74.

AGE_Nicolls
Must have misunderstood your problem.
As I don't uderstand why you want to add the timezone offset to two dates.
As long as your happy :)
If t1=6
ANd t2=7
Then the elapsed time is 1 + timezone offset ???
objects,

Your comment to take the duration in millis and write code to parse the individual time elements is a good suggestion:

long dutation = 36000;
long seconds = duration/1000;
long minutes = seconds/60;

However, it isn't quite that simple.  In order to get hours, minutes, seconds, and millisseconds, you have to write something like this:


long duration = 3612345;

// see how many whole hours are in duration
long hours = duration / 36000;
// remove millis used in hours
duration -= (hours * 36000)

// see how many whole minutes are left
long minutes = duration / 6000;
// remove millis used in minutes
duration -= (minutes * 6000)

// convert remaining millis to seconds as decimal
float seconds = duration / 1000;


I don't mind writing this code - but I'd rather use a class like Calendar to do this thatn maintain my own.  What happens if my hours equal 25?  Then I have to change my method to parse days out as well.  I can just use a Calendar object and it does everything for me.

Thanks for you input - I do appreciate it.

Regards,
AGE_Nicolls