You Kuper
You Kuper

Reputation: 1113

Convert integer minutes into String "hh:mm"

I need to convert minutes (defined as Integer) into the following String format "hh:mm" assuming that the startTime is "00:00". The below-given code is what I have so far, but it does not work properly. Also it does not take into account that the newTime should be shifted in accordance to startTime. Is there any other solution?

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);

Upvotes: 12

Views: 52520

Answers (8)

Sriram Umapthy
Sriram Umapthy

Reputation: 700

int minutes = 120;

int h = minutes / 60;
int m = minutes % 60;

String.format("%02d:%02d",h,m); // output : "02:00"
String.format("%d:%d",h,m); // output : "2:0"

Upvotes: 6

c24w
c24w

Reputation: 7856

I wrote this recently for converting seconds to hours, minutes and seconds; adapt it as you please (replace "s", "m", "h" with colons, omit seconds, etc).

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";
        }
    }
}

Upvotes: 3

apines
apines

Reputation: 1262

Here you go:

/*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 version

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)
        }

    }

}

Upvotes: 34

J-Jamet
J-Jamet

Reputation: 857

apines' answer is good but if you want to remove the warning in Android, add 'Locale' :

int hours = 3;
int minutes = 6;
private static final String TIME_SEPARATOR = ":";
String.format(Locale.getDefault(), "%02d"+TIME_SEPARATOR+"%02d", hours, minutes);

Upvotes: 1

Isaace
Isaace

Reputation: 141

int minutes = 129;
double hoursAndMinutes = (minutes / 60)
        + (double) minutes % 60 / 100;
String hoursAndMinutesStr = new DecimalFormat("0.00").format(hoursAndMinutes).replace('.', ':');
System.out.println(hoursAndMinutesStr);

Upvotes: -2

user425367
user425367

Reputation:

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;

Upvotes: 10

NickUnuchek
NickUnuchek

Reputation: 12857

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); 

RESULT: 24:04

Upvotes: -1

Gangnus
Gangnus

Reputation: 24464

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;

Upvotes: 0

Related Questions