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;
}
}
我被困住了,我不知道该怎么办。
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)
答案 0 :(得分:3)
%d
中有4个String.format()
,但只有3个值。删除最后一个%d
或添加其他值。
String time = String.format("%02d:%02d:%02d", s, m, h); // remove a %d
或
String time = String.format("%02d:%02d:%02d:%d", s, m, h, anotherVal); // add another value
答案 1 :(得分:1)
你不应该一起做这个!!只需使用SimpleDateFormat类就像这样:
long millis = 0;
Calendar c = Calendar.getInstance();
c.setTimeInMillis(millis);
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
System.out.println(sdf.format(c.getTime()));
只需用毫秒替换0即可。
答案 2 :(得分:0)
格式字符串中有4个“%d”但只提供3个参数...尝试删除最后一个%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;
}