Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Convert miliseconds to  minutes, and seconds

Posted on 2011-02-20
Medium Priority
Last Modified: 2012-05-11
I found this code to convert miliseconds to minutes, and seconds and I don't see how the calculations can work.

it seems to me the calculation would be

double time = miliseconds/1000000/60
int minutes = Integer.parseInt(time)

If I was using C rather than Java I would then do:

int seconds = (time-minutes)*100

To aquire the seconds

I don't know how to get an integer value for the seconds.  I am attaching the code I found and if anyone has an explaination as to how it would work or how I can get an integer of the remainder of my minute calculation I would appreciate it.  I don't see how the included code can work.
Cheers sarge

      public static void convertMillis(int Millis){
      String convert = String.format("You have %d hour(s), %d minute(s), and %d second(s)",
      Millis/(1000*60*60), (Millis%(1000*60*60))/(1000*60), ((Millis%(1000*60*60))%                 (1000*60))/1000);
Question by:sargent240
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
LVL 23

Accepted Solution

wdosanjos earned 2000 total points
ID: 34938400
Check if this works for you:

    public static void convertMillis(int Millis)
        int hours = Millis / (1000 * 60 * 60);

        Millis -= hours * 1000 * 60 * 60;

        int minutes = Millis / (1000 * 60);

        Millis -= minutes * 1000 * 60;

        int seconds = Millis / 1000;

        String convert = String.format("You have %d hour(s), %d minute(s), and %d second(s)",


Open in new window


Author Comment

ID: 34938491
This is the code I put in:

                 int Millis = Integer.parseInt(rec.getString("length").trim());

        int hours = Millis / (1000 * 60 * 60);
        Millis -= hours * 1000 * 60 * 60;
        minutes = Millis / (1000 * 60);
        Millis -= minutes * 1000 * 60;
        seconds = Millis / 1000;

The milliseconds value is coming from a database and it is the length of an audio file.  For example the value is 136333000 and if I calulate it in my old way I get 2.27.  If my thinking is correct that is ABOUT 2 minutes and 27 seconds.  When I used the above code It came to 52 minutes and 13 seconds.  I must have something screwed up.  What do ya think.

Author Comment

ID: 34938539
I think I discovered something.  The values I am using must not be milliseconds.  I is the valued that is returned from getting the durations of an audio file using the java audio functions.  I thought I read it was milliseconds but I must be wrong.  I used your code but substituted 1000000 for 1000 and it works.  Does that make sense.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 23

Expert Comment

ID: 34938558
Yes, it seems you have microseconds.

Author Comment

ID: 34938563
Thank you very much for your help!


Author Closing Comment

ID: 34938572
Good fast and congenial comunication

Featured Post

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

670 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