0

I was working on a HackerRank question where I turn time in 12 hour format (hour:min:sec am/pm) into 24 hour format (hour:min:sec) (eg hh:mm:ss) . I got everything done, with 3 int variables for hour, min, sec respectively and I want to turn them into a string to return. I use a string format like this

String str = String.format("%2d:%2d:%2d", hour, min, sec); but obviously does not work. I read the documentation on string formatter too but I try to get it work like what I was doing. Any suggestions?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161

1 Answers1

2

I think you are missing zero paddings.

Try

String str = String.format("%02d:%02d:%02d", hour, min, sec);
Naetmul
  • 14,544
  • 8
  • 57
  • 81