Help with convertMills

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jca613
    New Member
    • Mar 2010
    • 9

    Help with convertMills

    i have hard time doing this program.a example i have to is a string returning as hours:minutes:s econds, like convertMillis(5 500) returns a string 0:0:5, convertMillis(1 00000) returns a string, 0:140:. and convertMillis(5 55550000) 154:19:10. i am like having a brain bust lately and cant figure how to come make one and compile.Anyone help me? i have to make millseconds to hours:minutes:s econds
  • jca613
    New Member
    • Mar 2010
    • 9

    #2
    Converting Milliseconds to Time

    import java.util.*;
    public class hw_6 {
    public static void main(String[] args){
    }

    public static void convertMillis(l ong millis){
    System.out.prin t("How much Millseconds ");
    long Millis =0;
    long time = Millis / 1000;
    int seconds = ((int)(time % 60));
    int minutes = ((int)((time % 3600) / 60));
    int hours = ((int)(time / 3600));

    }
    }

    okay i got part of it right i think. i am trying prompt user to end amount of milliseconds so that it will convert into time.

    in this fromat(hh:mm:ss . please help me as quickly as possible please. the more i understand then better.

    Comment

    • jkmyoung
      Recognized Expert Top Contributor
      • Mar 2006
      • 2057

      #3
      Do you care about rounding, or are you just rounding down?
      Are you going smallest units to biggest units, or the other way around?
      eg. smallest first:
      5500ms / 1000 => 5 seconds

      100000ms / 1000 => 100 seconds
      100 seconds is bigger than 60 secs (in a minute), so count the number of minutes too.

      Code would help

      Comment

      • jca613
        New Member
        • Mar 2010
        • 9

        #4
        it doesn't matter i guess. as long it turns out like something like this 154:19:10 when the progarm is done. so it being hours:Minutes:S econds

        Comment

        • jca613
          New Member
          • Mar 2010
          • 9

          #5
          mport java.util.*;
          public class hw_6 {
          public static void main(String[] args){
          }

          private static String convertMillis(l ong millis) {
          System.out.prin t("How much Millseconds ");
          long time = millis / 1000;
          int seconds = ((int)(time % 60));
          int minutes = ((int)((time % 3600) / 60));
          int hours = ((int)(time / 3600));
          return convertMillis(0 ) ;
          }

          private static final StringBuilder(S tring convertedTime){
          return convertedTime.t oString();
          StringBuffer sb = new StringBuffer();

          sb.append(hours );
          sb.append(":");
          sb.append(minut es);
          sb.append(":");
          sb.append(secon ds);
          String s = sb.toString();

          }
          {
          }
          }
          this what i got so far? did i do something wrong? or am i missing something. and do i need to add a assert ..:..:..".equal s(convertMillis (....) into it?

          Comment

          • jkmyoung
            Recognized Expert Top Contributor
            • Mar 2006
            • 2057

            #6
            This is good so far. Now you just need to organize your code to call each other properly.
            Code:
            private static String convertMillis(long millis) {
              ...
              ...
              int hours = ((int)(time / 3600));
            
              //create return string.
              StringBuffer sb = new StringBuffer();
              sb.append(hours);
              ...
              ...
              return sb.toString();
            }
            and in your main, you'd have something like (pseudocode):
            Code:
            public static void main(String[] args){
              System.out.println("How much Millseconds ");
              get millis from input.
              System.out.println(convertMillis(millis) );
            }

            Comment

            • jca613
              New Member
              • Mar 2010
              • 9

              #7
              import java.util.*;
              public class hw_6 {

              public static void main(String[] args){

              System.out.prin tln("How much Millseconds ");

              get millis; from input;


              System.out.prin tln(convertMill is(millis) );
              }
              //return string

              private static String convertMillis(l ong millis) {
              StringBuffer sb = new StringBuffer();
              sb.append(hours );
              sb.append(":");
              sb.append(minut es);
              sb.append(":");
              sb.append(secon ds);
              return sb.toString();
              long time = millis / 1000;

              int seconds = ((int)(time % 60));
              int minutes = ((int)((time % 3600) / 60));
              int hours = ((int)(time / 3600));
              return convertMillis(0 ) ;
              }

              private static final StringBuilder(S tring convertedTime){
              return convertedTime.t oString();


              }

              i dont know if that is what you mean by more organized. i am still getting errors. for example.
              private static final StringBuilder(S tring convertedTime){ <-- there it says i cant find the return string and stuff

              i get errors on the pseudocode.

              Comment

              • jkmyoung
                Recognized Expert Top Contributor
                • Mar 2006
                • 2057

                #8
                In your convertMillis function, you're using hours before you set hours! Set the hours before you use it.

                pseudocode: Not real, syntactically correct code. -> figure it out.

                Comment

                • jca613
                  New Member
                  • Mar 2010
                  • 9

                  #9
                  thanks well i got it done

                  Comment

                  Working...