简体   繁体   中英

Fastest way to format a String using concatenation and manipulation

String temp = "";

temp = String.format("%02d", ""+hour)+":"+String.format("%02d", ""+min)+":"+String.format("%02d", ""+sec);

Is this the fastest way to format the numbers with concatenation to specify leading zeroes, or is there another way?

I have to display the String in the format 00:00:00. The hour, min, and sec are int variables which start from 0 and are put into the Thread which counts the time elapsed and displays this time accordingly. Is this the correct way, or is there any better way?

Try this. I assume that hour, min and sec are ints.

String temp = "";
temp = String.format("%02d:%02d:%02d", hour, min, sec);

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM