3 Replies Latest reply on Jan 22, 2004 12:03 AM by brucechapman

# Milliseconds to seconds

Hello people,

I just started learning java, I have a little problem that I can't seem to solve. I have a small program that outputs milliseconds. But I want to convert this milliseconds to seconds, this is where am having sleepless nights.

this is a segment of the code.

long start1 = System.currentTimeMillis();
long end = System.currentTimeMillis();
long elapsedTimeMillis = end - start1 ;
System.out.println(elapsedTimeMillis );
and i got an output like this: 1072215156330.

but, how do i convert this output into seconds.

Cheers
Omega.
• ###### 1. Re: Milliseconds to seconds
1 sec = 1000 millisec

so divide the result by 1000 to get the seconds

long elapsedTimeSecond = elapsedTimeMillis / 1000; // total # of seconds
long elapsedTimeMillis = elapsedTimeMillis % 1000; // total number of millis left

System.out.println("Seconds = " + elapsedTimeSecond + " millis = " + elapsedTimeMillis);
• ###### 2. Re: Milliseconds to seconds
use the Calendar, DateFormat SimpleDateFormat class to convert time and date stuff ..

brgds Per Fisker
• ###### 3. Re: Milliseconds to seconds
Hello people,

I just started learning java, I have a little problem
that I can't seem to solve. I have a small program
that outputs milliseconds. But I want to convert this
milliseconds to seconds, this is where am having
sleepless nights.

this is a segment of the code.

long start1 = System.currentTimeMillis();
long end = System.currentTimeMillis();
long elapsedTimeMillis = end - start1 ;
System.out.println(elapsedTimeMillis );
and i got an output like this: 1072215156330.
But that output time in millis corresponds to about December 24 last year, so I doubt very much that is the difference between end and start1 unless you started the program at the epoch (1970) (before java existed!). Maybe thats why you asked such an apparently stupid question because the obvious answer of dividing by 1000 was not going to yield an answer anywhere within cooey of what you might expect your answer to be.