String.format 不提供超过一小时的尺寸的正确格式
String.format does not deliver the correct format for sizes larger than one hour
我想将 3600000 毫秒转换为 01:00:00 格式(表示 01 小时:00 分钟:00 秒)。不幸的是它给了我 2:120:00。在 60,000 毫秒,他给了我 00:10:00,这是正确的结果。直到 00:59:00 他输出格式化的数字,但是超过一个小时的所有内容不再以正确的格式输出它
public static String formattedTime(long time) {
int hours = (int) (time / 1000) / 60 / 60;
int minutes = (int) (time / 1000) / 60;
int seconds = (int) (time / 1000) % 60;
return String.format(Locale.getDefault(), "%02d:%02d:%02d", hours, minutes, seconds);
}
按如下操作:
import java.util.Locale;
public class Main {
public static void main(String[] args) {
// Tests
System.out.println(formattedTime(3600000));
System.out.println(formattedTime(4580000));
}
public static String formattedTime(long time) {
// Convert time in milliseconds to seconds
int milliToSec = (int) (time / 1000);
// Get hours from total seconds
int hours = milliToSec / 3600;
// Get minutes from total seconds
int minutes = (milliToSec / 60) % 60;
// Get remaining seconds from total seconds
int seconds = milliToSec % 60;
// Return formatted string
return String.format(Locale.getDefault(), "%02d:%02d:%02d", hours, minutes, seconds);
}
}
输出:
01:00:00
01:16:20
我想将 3600000 毫秒转换为 01:00:00 格式(表示 01 小时:00 分钟:00 秒)。不幸的是它给了我 2:120:00。在 60,000 毫秒,他给了我 00:10:00,这是正确的结果。直到 00:59:00 他输出格式化的数字,但是超过一个小时的所有内容不再以正确的格式输出它
public static String formattedTime(long time) {
int hours = (int) (time / 1000) / 60 / 60;
int minutes = (int) (time / 1000) / 60;
int seconds = (int) (time / 1000) % 60;
return String.format(Locale.getDefault(), "%02d:%02d:%02d", hours, minutes, seconds);
}
按如下操作:
import java.util.Locale;
public class Main {
public static void main(String[] args) {
// Tests
System.out.println(formattedTime(3600000));
System.out.println(formattedTime(4580000));
}
public static String formattedTime(long time) {
// Convert time in milliseconds to seconds
int milliToSec = (int) (time / 1000);
// Get hours from total seconds
int hours = milliToSec / 3600;
// Get minutes from total seconds
int minutes = (milliToSec / 60) % 60;
// Get remaining seconds from total seconds
int seconds = milliToSec % 60;
// Return formatted string
return String.format(Locale.getDefault(), "%02d:%02d:%02d", hours, minutes, seconds);
}
}
输出:
01:00:00
01:16:20