Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert integer minutes into String "hh:mm"

Tags:

java

time

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);
like image 918
You Kuper Avatar asked Jan 18 '12 19:01

You Kuper


People also ask

How do you convert minutes to HH MM SS in SQL?

Multiplying your number by 60,000 converts your number to milliseconds. Adding that number of milliseconds to the base date creates an actual DATETIME datatype containing a date of 1900-01-01 with the correct time in hours, minutes, seconds, a milliseconds with a resolution of 3.3 milliseconds.

How do I convert string to minutes?

Split the string into its component parts. Get the number of minutes from the conversion table. Multiply that by the number and that is the number of minutes. Convert that to whatever format you need for the display.


1 Answers

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"

like image 141
Sriram Avatar answered Sep 18 '22 04:09

Sriram