Solved

Convert miliseconds to  minutes, and seconds

Posted on 2011-02-20
6
1,436 Views
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);
      System.out.println(convert);
      }
0
Comment
Question by:sargent240
  • 4
  • 2
6 Comments
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
Comment Utility
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)",
            hours,
            minutes,
            seconds);

        System.out.println(convert);
    }

Open in new window

0
 

Author Comment

by:sargent240
Comment Utility
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.
Cheers
0
 

Author Comment

by:sargent240
Comment Utility
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.
Cheers!
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 23

Expert Comment

by:wdosanjos
Comment Utility
Yes, it seems you have microseconds.
0
 

Author Comment

by:sargent240
Comment Utility
Thank you very much for your help!

Cheers
0
 

Author Closing Comment

by:sargent240
Comment Utility
Good fast and congenial comunication
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now