Calculate millis to hours, minutes and seconds

Hello,Please could somebody help me to convert a long millisecond value into int's for hours, minutes and seconds?Thankyou in advance.p.s. So far I have done long seconds = millis / 1000; :-)
[262 byte] By [nleaver] at [2007-9-27 18:45:18]
# 1
you are taking the **ss arn't you?
Abuse at 2007-7-6 20:00:47 > top of Java-index,Other Topics,Algorithms...
# 2
No...serious...
nleaver at 2007-7-6 20:00:47 > top of Java-index,Other Topics,Algorithms...
# 3

It's kind of funny to compare this thread with the first oneint seconds = (int)(millis / 1000);

int minutes = seconds / 60;

int hours = minutes / 60;

Jetset_Willy at 2007-7-6 20:00:47 > top of Java-index,Other Topics,Algorithms...
# 4
Oops the threads are listed in reverse chronological order aren't they? I should say it's funny to compare this thread to the one by YATArchivist.
Jetset_Willy at 2007-7-6 20:00:47 > top of Java-index,Other Topics,Algorithms...
# 5

Thankyou for replying, however the code you gave me goves the total hours for the whole time, total minutes for the whole time and total seconds for the whole time.

What I am trying to do is to get the hours, minutes and seconds, which in total equal the amount of millis.

So for 60000 millis I would get 0 hours, 1 minutes and 0 seconds..;.. thanks again.

nleaver at 2007-7-6 20:00:47 > top of Java-index,Other Topics,Algorithms...
# 6

long days = milis / (1000 * 60 * 60 * 24);

milis-= days * (1000 * 60 * 60 * 24);

long hrs= milis / (1000 * 60 * 60);

milis-= hrs* (1000 * 60 * 60);

long mins = milis / (1000 * 60);

milis-= mins * (1000 * 60);

long secs = milis / (1000);

milis-= secs * (1000);

plucien at 2007-7-6 20:00:47 > top of Java-index,Other Topics,Algorithms...
# 7
Wont you lose accuracy at each stage of this calculation. Isn't it better to work with doubles then convert the results to ints afterwards.If i'm being stupid just ignore this post.
GiddingsR at 2007-7-6 20:00:47 > top of Java-index,Other Topics,Algorithms...
# 8
The "lost of accuracy" is the desired behaviour.I'm not ignoring your post :)
plucien at 2007-7-6 20:00:47 > top of Java-index,Other Topics,Algorithms...
# 9

Dear friends,

I think the one below is the most accurate method since it uses the java.util.Calendar class. You can try to input different miliseconds and you will see how amazingly it works!

public static void timeTest(long milisecond) {

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT")); // get the GMT calendar

cal.clear(); // clear all fields

Date date = new Date(milisecond);

cal.setTime(date);// set the cal to miliseconds

// print the hour, minute, seconds, miliseconds

System.out.println("hour = " + cal.get(Calendar.HOUR) + ", minute = " + cal.get(Calendar.MINUTE) +

", second = " + cal.get(Calendar.SECOND) + ", milisecond = " + cal.get(Calendar.MILLISECOND));

}

thunderBolt at 2007-7-6 20:00:47 > top of Java-index,Other Topics,Algorithms...
# 10

do you mean like

100d 5h 13m 40s == 86587840000

therefore

double timeLeft = 86587840000 / 8.64e7; /* milliSec pre day */

int days = Math.Floor(timeLeft);

timeLeft -= (double)days;

timeLeft *= 24; /* hours per day */

int hours = Math.Floor(timeLeft);

timeLeft -= (double)hours;

timeLeft *= 60; /* minutes per hour */

int minutes = Math.Floor(timeLeft);

timeLeft -= (double)minutes;

timeLeft *= 60; /* seconds per minute */

int seconds = Math.Floor(timeLeft);

you can arrange this so you go up from seconds to days as well but the

key is that the time in the example is 100.2174074 days so take the 0.2174...days and get the hours etc. etc. using the Math.Floor()

radix_zero at 2007-7-6 20:00:47 > top of Java-index,Other Topics,Algorithms...
# 11

Hia,

Are you sure that person bove this is right? Isn't 100days 5hours 13minutes 40seconds equal to 8658820000ms?

Anyway, I reckon this way runs better without calls to package classes.

// this is what it really is for 100days 5hours 13minutes 40seconds

long total = 8658820000;

final long dayMillis = 24*60*60*1000;

final long hourMillis = 60*60*1000;

final long minMillis = 60*1000;

final long secMillis = 1000;

long days = total/dayMillis;/* get the number of days */

total = total%dayMillis;/* and the remainder */

long hours = total/hourMillis;/* do the same for the rest */

total = total%hourMillis;

long mins = total/minMillis;

total = total%minMillis;

long secs = total/secMillis;

System.out.println(days+" : "+hours+" : "+mins+" : "+secs+" ");

tadahh.

/*************/

I'm always amazed to hear of air crash victims so badly mutilated that they have to be identified by their dental records. What I can't understand is, if they don't know who you are, how do they know who your dentist is?

mightymightyeddturnip at 2007-7-6 20:00:47 > top of Java-index,Other Topics,Algorithms...
# 12

> I'm always amazed to hear of air crash victims so

> badly mutilated that they have to be identified by

> their dental records. What I can't understand is, if

> they don't know who you are, how do they know who your

> dentist is?

They know who you might be -- they have the passenger list. So they send whatever it is to the dentists of all the unidentified passengers. At least, that's what I would do in their place.

DrClap at 2007-7-6 20:00:47 > top of Java-index,Other Topics,Algorithms...
# 13

long millis = ...;

long sec = (millis/1000)%60;

long min = (millis/60/1000)%60;

long hrs = (millis/60/60/1000);

or

long millis = ...;

long sec = (millis/1000)%60;

long min = (millis/60/1000)%60;

long hrs = (millis/60/60/1000)%24;

long day = (millis/24/60/60/1000);

or

long millis = ...;

long sec = (millis/1000)%60;

long min = (millis/60/1000)%60;

long hrs = (millis/60/60/1000)%24;

long day = (millis/24/60/60/1000)%7;

long wks = (millis/7/24/60/60/1000);

Mike40033 at 2007-7-6 20:00:47 > top of Java-index,Other Topics,Algorithms...
# 14
Hi All,Thanks for all your help. I have now used the examples you have given me to complete my task. Sorry I couldn't give everyone dukes, I only had 2 left :-(Thanks again.
nleaver at 2007-7-6 20:00:47 > top of Java-index,Other Topics,Algorithms...
# 15
Hey, Dr Clap mate.. IT WAS A JOKE! I understand what happens in reality. I thought that someone out there may find it funny, hence I added it. Maybe it's just my sense of humour.Thanks anyway.Eddy.
mightymightyeddturnipa at 2007-7-18 14:09:20 > top of Java-index,Other Topics,Algorithms...
# 16
I bet he was joking too.
Jetset_Willya at 2007-7-18 14:09:20 > top of Java-index,Other Topics,Algorithms...