-5
import java.util.Scanner;

public class Mills {

    public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    System.out.println("Enter milliseconds");
    long mills= sc.nextLong();

    System.out.println(convertMillis(mills));

    }

    public static String convertMillis(long millis){

        long s = (millis / 1000) % 60;
        long m = (millis / (1000 * 60)) % 60;
        long h = (millis / (1000 * 60 * 60)) % 24;
        String time = String.format("%02d:%02d:%02d:%d",s,m,h);
        return time;
    }
}

I am stuck, I don't know what to do after.

Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier 'd'
    at java.util.Formatter.format(Unknown Source)
    at java.util.Formatter.format(Unknown Source)
    at java.lang.String.format(Unknown Source)
    at Mills.convertMillis(Mills.java:19)
    at Mills.main(Mills.java:10)
Indra Yadav
  • 600
  • 5
  • 22
  • check this http://stackoverflow.com/questions/625433/how-to-convert-milliseconds-to-x-mins-x-seconds-in-java – A Paul Feb 04 '14 at 06:05
  • @APaul Looks like a duplicate to me... – sashkello Feb 04 '14 at 06:06
  • yes. Thats why I mentioned to check this. He has done a structure error that he can figure out from this post. – A Paul Feb 04 '14 at 06:13
  • possible duplicate of [java convert milliseconds to time format](http://stackoverflow.com/questions/4142313/java-convert-milliseconds-to-time-format) – earthmover Feb 04 '14 at 06:25

3 Answers3

3

You have 4 %d in the String.format() but only 3 values. Remove the last %d or add another value.

String time = String.format("%02d:%02d:%02d", s, m, h); // remove a %d

or

String time = String.format("%02d:%02d:%02d:%d", s, m, h, anotherVal); // add another value
Rahul
  • 44,383
  • 11
  • 84
  • 103
1

You should not have to do this all together!! Just use SimpleDateFormat class Like this:

    long millis = 0;
    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(millis);
    SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
    System.out.println(sdf.format(c.getTime()));

Just replace the 0 with your millis.

Mostafa Zeinali
  • 2,456
  • 2
  • 15
  • 23
  • Just remember that Calendar's reference date is the "Epoch" which is January 1st 1970 00:00:00. The date will be calculated relative to that, but if you are in the range of less than 24 hours, this should work fine... – Mostafa Zeinali Feb 04 '14 at 06:17
0

You have 4 "%d"s in the format string but provide only 3 arguments... Try removing the last %d

public static String convertMillis(long millis){

    long s = (millis / 1000) % 60;
    long m = (millis / (1000 * 60)) % 60;
    long h = (millis / (1000 * 60 * 60)) % 24;
    String time = String.format("%02d:%02d:%02d",s,m,h);
    return time;
}
Mostafa Zeinali
  • 2,456
  • 2
  • 15
  • 23