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;
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.
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);
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));
}
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()
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?
> 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.
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);