Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

I need to convert minutes to hours and minutes in java. For example 260 minutes should be 4:20. can anyone help me how to do convert it.

like image 395
lakshmi Avatar asked Mar 22 '11 05:03

lakshmi


People also ask

How do you convert minutes to hours and minutes?

There are 60 minutes in 1 hour. To convert from minutes to hours, divide the number of minutes by 60. For example, 120 minutes equals 2 hours because 120/60=2.

How do you convert time in Java?

mport java. util. Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int days=24; int hours = 60; int mins=60; int res=days*hours*mins; System.


1 Answers

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.

like image 64
Federico klez Culloca Avatar answered Oct 07 '22 05:10

Federico klez Culloca