我需要将minutes
(定义为Integer)转换为以下字符串格式“hh:mm”,假设startTime
为“00:00”。下面给出的代码是我到目前为止,但它无法正常工作。此外,它没有考虑newTime
应根据startTime
进行转移。还有其他解决方案吗?
String startTime = "00:00";
int minutes = 120;
double time = minutes/60;
String timeS = Double.toString(time);
String[] hourMin = timeS.split(".");
String h = hourMin[0];
String m = hourMin[1];
String newTime = "";
newTime.concat(h+":"+m);
答案 0 :(得分:24)
你走了:
/*Output: 02:05 */
public String fromMinutesToHHmm(int minutes) {
long hours = TimeUnit.MINUTES.toHours(Long.valueOf(minutes));
long remainMinutes = min - TimeUnit.HOURS.toMinutes(hours);
return String.format("%02d:%02d", hours, remainMinutes);
}
Kotlin 版本
class DateUtils {
companion object {
fun fromMinutesToHHmm(minutes: Int): String {
val hours = TimeUnit.MINUTES.toHours(minutes.toLong())
val remainMinutes = minutes - TimeUnit.HOURS.toMinutes(hours)
return String.format("%02d:%02d", hours, remainMinutes)
}
}
}
答案 1 :(得分:8)
String startTime = "00:00";
int minutes = 120;
int h = minutes / 60 + Integer.parseInt(startTime.substring(0,1));
int m = minutes % 60 + Integer.parseInt(startTime.substring(3,4));
String newtime = h+":"+m;
答案 2 :(得分:2)
我最近写了这篇文章,将秒数转换为小时,分钟和秒;根据需要调整它(用冒号替换“s”,“m”,“h”,省略秒等)。
编辑:在匆忙改进之后修复我的代码中的愚蠢错误。
private static String getFormattedTime(int secs) {
// int secs = (int) Math.round((double) milliseconds / 1000); // for millisecs arg instead of secs
if (secs < 60)
return secs + "s";
else {
int mins = (int) secs / 60;
int remainderSecs = secs - (mins * 60);
if (mins < 60) {
return (mins < 10 ? "0" : "") + mins + "m "
+ (remainderSecs < 10 ? "0" : "") + remainderSecs + "s";
}
else {
int hours = (int) mins / 60;
int remainderMins = mins - (hours * 60);
return (hours < 10 ? "0" : "") + hours + "h "
+ (remainderMins < 10 ? "0" : "") + remainderMins + "m "
+ (remainderSecs < 10 ? "0" : "") + remainderSecs + "s";
}
}
}
答案 3 :(得分:2)
private static string calculateReward(string answer, string reward)
{
reward = answer == "1" ? "a cat" : answer == "2" ? "power to rival Chuck Norris" : answer == "3" ? "a burger" : "nothing";
return reward;
}
答案 4 :(得分:0)
String startTime = "00:00";
int minutes = 120;
//--- counting
int hour = minutes/60;
minutes=minutes-hours*60;
String m = Integer.toString(minutes);
String h = Integer.toString(hours);
newTime=h+":"+m;
答案 5 :(得分:0)
int ALLTIME_IN_MINUTES = 1444
int hour = ALLTIME_IN_MINUTES /60;
int min = ALLTIME_IN_MINUTES %60;
String dateStr = hour+":"+min;
SimpleDateFormat curFormater = new SimpleDateFormat("H:m");
Date dateObj = curFormater.parse(dateStr);
SimpleDateFormat postFormater = new SimpleDateFormat("HH:mm");
String newDateStr = postFormater.format(dateObj);
<强>结果: 24:04 强>
答案 6 :(得分:0)
apines的答案很好但是如果你想在Android中删除警告,请添加'Locale':
int hours = 3;
int minutes = 6;
private static final String TIME_SEPARATOR = ":";
String.format(Locale.getDefault(), "%02d"+TIME_SEPARATOR+"%02d", hours, minutes);
答案 7 :(得分:-2)
int minutes = 129;
double hoursAndMinutes = (minutes / 60)
+ (double) minutes % 60 / 100;
String hoursAndMinutesStr = new DecimalFormat("0.00").format(hoursAndMinutes).replace('.', ':');
System.out.println(hoursAndMinutesStr);