How to Convert Minutes to Hours and Minutes (Hh:Mm) in Java

How to convert minutes to Hours and minutes (hh:mm) in java

If your time is in a variable called t

int hours = t / 60; //since both are ints, you get an int
int minutes = t % 60;
System.out.printf("%d:%02d", hours, minutes);

It couldn't get easier

Addendum from 2021:

Please notice that this answer is about the literal meaning of the question: how to convert an amount of minute to hours + minutes. It has nothing to do with time, time zones, AM/PM...

If you need better control about this kind of stuff, i.e. you're dealing with moments in time and not just an amount of minutes and hours, see Basil Bourque's answer below.

JAVA convert minutes into default time [hh:mm:ss]

EDIT: To show the seconds as SS you can make an easy custom formatter variable to pass to the String.format() method

EDIT: Added logic to add one minute and recalculate seconds if the initial double value has the number value after the decimal separator greater than 59.

EDIT: Noticed loss of precision when doing math on the double (joy of working with doubles!) seconds, so every now and again it would not be the correct value. Changed code to properly calculate and round it. Also added logic to treat cases when minutes and hour overflow because of cascading from seconds.

Try this (no external libraries needed)

public static void main(String[] args) {
final double t = 1304.00d;

if (t > 1440.00d) //possible loss of precision again
return;

int hours = (int)t / 60;
int minutes = (int)t % 60;
BigDecimal secondsPrecision = new BigDecimal((t - Math.floor(t)) * 100).setScale(2, RoundingMode.HALF_UP);
int seconds = secondsPrecision.intValue();

boolean nextDay = false;

if (seconds > 59) {
minutes++; //increment minutes by one
seconds = seconds - 60; //recalculate seconds
}

if (minutes > 59) {
hours++;
minutes = minutes - 60;
}

//next day
if (hours > 23) {
hours = hours - 24;
nextDay = true;
}

//if seconds >=10 use the same format as before else pad one zero before the seconds
final String myFormat = seconds >= 10 ? "%d:%02d:%d" : "%d:%02d:0%d";
final String time = String.format(myFormat, hours, minutes, seconds);
System.out.print(time);
System.out.println(" " + (nextDay ? "The next day" : "Current day"));
}

Of course this can go on and on, expanding on this algorithm to generalize it. So far it will work until the next day but no further, so we could limit the initial double to that value.

 if (t > 1440.00d)
return;

How to convert minutes to HH:mm on android?

Check this for reference Minutes to Hours-Minutes..

Java convert minutes into hours minutes

Use String.format to format the output:

System.out.println(String.format("%02d", minnutesRemaining ));

Convert integer minutes into String "hh:mm"

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;

Individually available Hour Minute Seconds to HH:mm:ss format

Something like this:

String.format("%02d:%02d:%02d", e.getHours(), e.getMinutes(), e.getSeconds())

how to convert minutes to days,hours,minutes

A shorter way. (Assumes time >= 0)

 public String timeConvert(int time) { 
return time/24/60 + ":" + time/60%24 + ':' + time%60;
}


Related Topics



Leave a reply



Submit