I have two int variables as follows:
int minutes = 20;
int hours = 8;
How do I want to convert them to "HHMM" formation?
For the above, the result should be "0820".
If you just want to pad the variables to 2 digits each:
int minutes = 20;
int hours = 8;
String str = String.format("%02d%02d", hours, minutes);
System.out.println(str);
Prints:
0820
Look at this for more details.
Use Calendar
class as below:
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR, hours);
c.set(Calender.MINUTE, minutes);
In case you only want to format the values, you can use:
String str = String.format("%02d%02d", hours, minutes);
Looks like you want it in a string which is simple. Just append them to a string.
String time = String.valueOf(hours) + String.valueOf(minutes);
if (hours < 10)
time = "0" +time;
如果您只想显示“0820”,可以将它们粘贴在一个字符串中。
String hoursMinutes = String.format("%02d", hours) + String.format("%02d", minutes);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.