• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 286
  • Last Modified:

Getting hours and minutes from a milisecond value

Hi all, very easy question!

I have a millisecond value which indicates the time left a program has to run.  How do I convert this into a String of the format:  hours/mins/seconds
0
agrees
Asked:
agrees
  • 2
  • 2
1 Solution
 
brettmjohnsonCommented:
There are 1000 milliseconds in a second, 60 seconds in a minute, 60 minutes in an hour.

static final long MSecsPerHour =  60*60*1000L;
static final long MSecsPerMin = 60*1000L;
static final long MSecsPerSec = 1000L;

long msecs;
int hours, minutes, seconds;

hours = msecs/MSecsPerHour;
msecs %= MSecsPerHour;
minutes = msecs/MSecsPerMin;
msecs %= MSecsPerMin;
seconds =  msecs/MSecsPerSec;







0
 
agreesAuthor Commented:
Is there not a ready made utility function?
0
 
brettmjohnsonCommented:
None that I know of.  The math is pretty trivial.  
It is unlikely that there is a standard library function for  an infrequently used,
yet trivial to calculate transformation.
0
 
sciuriwareCommented:
What you are looking for is in the Calendar class, but you CAN divide yourself, can't you?

;JOOP!
0
 
agreesAuthor Commented:
>  What you are looking for is in the Calendar class, but you CAN divide yourself, can't you?

There are plenty of existing Java library functions which I could write myself.  But if it exists already then there are many reasons to use the existing function and NOT write your own.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now