(Convert milliseconds to hours, minutes, and seconds) Write a method that converts milliseconds to hours, minutes and seconds using the following header:
public static String convertMillis(long millis)
The method returns a string as hours:minutes:seconds. For example, convertMillis(5500) returns a string 0:0:5, convertMillis(100000) returns a string 0:1:40, convertMillis(555550000) returns a string 154:19:10.
ConvertMil.java
/** * Convert milliseconds to hours, minutes, and seconds. */ import javax.swing.JOptionPane; // import JOptionpane. public class ConvertMil { // Main Method. public static void main(String[] args) { long millisec = 0; // Promt to user to enter milliseconds. while(millisec <= 0){ millisec = Long.parseLong(JOptionPane.showInputDialog("Please enter the milliseconds:")); } String time = convertMillis(millisec); // convert milliseconds. prntStr(millisec+" milliseconds is equal to "+ time); // Print hour:munite:second } // End of main method. /** Convert Millisecond to hour:munite:second */ public static String convertMillis(long millis){ long sumsecond,second,summunite,munite,hour; // Declare variables. sumsecond = millis/1000; // sum of second second = sumsecond%60; // munite summunite = sumsecond/60; // sum of munite munite = summunite%60; // munite hour = summunite/60; // hour String result = hour+":"+munite+":"+second; return result; } // End of convertMillis method. /** Print String */ public static void prntStr(String print){ System.out.print(print); } // Enf of prntStr method. } // End of public class.
Yorum Yok:
Yorum Yap:
Yorum yapabilmek için giriş yapmalısınız.