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);
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.
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.
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With