Solved

# Getting hours and minutes from a milisecond value

Posted on 2004-09-11
277 Views
Last Modified: 2008-02-01
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
Question by:agrees
• 2
• 2
5 Comments

LVL 23

Expert Comment

ID: 12035008
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

Author Comment

ID: 12035103
Is there not a ready made utility function?
0

LVL 23

Accepted Solution

brettmjohnson earned 50 total points
ID: 12035296
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

LVL 24

Expert Comment

ID: 12035446
What you are looking for is in the Calendar class, but you CAN divide yourself, can't you?

;JOOP!
0

Author Comment

ID: 12035688
>  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

## Join & Write a Comment Already a member? Login.

### Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

#### 743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

#### Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!