Solved

Getting hours and minutes from a milisecond value

Posted on 2004-09-11
5
280 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
Comment
Question by:agrees
  • 2
  • 2
5 Comments
 
LVL 23

Expert Comment

by:brettmjohnson
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

by:agrees
ID: 12035103
Is there not a ready made utility function?
0
 
LVL 23

Accepted Solution

by:
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

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

;JOOP!
0
 

Author Comment

by:agrees
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Android studio getdrawable(int) is deprecated 4 113
servlet filter example 37 71
iterator/ListIterator approach 17 39
SequenceInputStream example 3 19
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

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

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

Join & Ask a Question