3 Replies Latest reply: Jan 21, 2004 6:03 PM by 608410 RSS

    Milliseconds to seconds

    807582
      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.

      Please help me out guys and have a great holiday.

      Cheers
      Omega.
        • 1. Re: Milliseconds to seconds
          807582
          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
            807582
            use the Calendar, DateFormat SimpleDateFormat class to convert time and date stuff ..

            brgds Per Fisker
            • 3. Re: Milliseconds to seconds
              608410
              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.

              Hopefully this solves your problem rather than just answering your question.

              >
              but, how do i convert this output into seconds.

              Please help me out guys and have a great holiday.

              Cheers
              Omega.