Reputation: 97
I have this method in my code to print for a clock. I am still only getting 1 digit of an output even though i need 2. There are purposely no params. I am trying to get the format 12:01:50 PM
public void printStandard() {
if (hrs < 12) {
System.out.printf("%2d:%2d:%2d AM%n", hrs, mins, secs);
} else {
System.out.printf("%2d:%2d:%2d PM%n", hrs - 12, mins, secs);
}
}
Upvotes: 0
Views: 623
Reputation: 23012
You should use %02d
instead to have 07
instead of 7
in your time String
. It will add padding of 0
in the number if number is less than two digits.
i.e
System.out.printf("%02d:%02d:%02d AM%n", 7, 1, 5);
OUTPUT
07:01:05 AM
NOTE : You can also take a look at SimpleDateFormat
and Calendar
if you are working with Date
.
Upvotes: 1