Pratik
Pratik

Reputation: 30855

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?

Upvotes: 1

Views: 105

Answers (1)

Augusto
Augusto

Reputation: 29867

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

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

Upvotes: 9

Related Questions