Formatting Output with printf() in Java

Java.io package contains a PrintStream class with two formatting methods: format() and printf().  This tutorial will teach formatting output with printf() in Java. Here, we will first deconstruct the printf() syntax. After that, we will practice a few of the examples through which you will learn to format string, number, boolean, date, and time in java.

Deconstructing printf()

The printf() method can be used in the following three ways.

System.out.printf(string);
System.out.printf(format, arguments);
System.out.printf(locale, format, arguments);
  • In the first method, we can directly output the string as it is.
  • In the second method, we specify the format in which we want our string to be displayed. The format parameter may contain text along with format specifiers that format the arguments, which is the second parameter in the print method. The arguments parameter contains the values for the placeholders in the format string.
  • Lastly, in the third method, there is a parameter locale through which we can format our output for different locales.

Now let’s dive deeper into the format parameter. Format parameter consists of literals and format specifiers. Format specifiers consist of flags, width, precision, and converters in the following order.

%[flags][width][.precision]converter

Note that [flags], [width], and [.precision] specifiers are optional here. Let’s have a look at them one by one.

  • Through [flags], we can play with how our output is displayed on the screen.
  • [width] allocates a minimum number of characters that will be used to display the output.
  • [.precision] defines the number of digits of precision.
  • The converter formats the arguments and is mandatory. The converters for a few of the data types are as follows.
Converters Data type
d Decimal Integers
c Characters
e Exponential Floating-point numbers
f Floating-point numbers
t Date/Time
s String

Format specifiers have a % sign along with a converter that tells about the type of argument to be formatted. In between the % sign and the converter you can have optional specifiers that include [flags], [width], and [precision]. We will have a look at examples showing the use of flags, width, precision, and converters later in the tutorial.

Format a String with printf()

Now let’s have a look at a few examples of String formatting using printf() in java. The following code snippet uses %s to print string.

 System.out.printf("%s", "Welcome to Apps Developer Blog!" );

The output of the above code is

Welcome to Apps Developer Blog!

If we use %S, the output will be in the upper case.

System.out.printf("%S", "Welcome to Apps Developer Blog!" );
WELCOME TO APPS DEVELOPER BLOG!

Now let’s use the width specifier. Here the output will have 40 characters.

System.out.printf("'%40s'", "Welcome to Apps Developer Blog!" );

The output will be

'         Welcome to Apps Developer Blog!'

If we add the flag specifier i.e. , the output will be left aligned.

System.out.printf("'%-40s'", "Welcome to Apps Developer Blog!" );

The output will be

'Welcome to Apps Developer Blog!         '

Format a Number with printf()

Now let’s have a look at a few examples of Number formatting using printf() in java. This is how we can print an integer using %d.

System.out.printf("I read %d blog a day", 1);

The output will be

I read 1 blog a day

Now let’s use the width specifier with an integer along with a 0 flag. The resulting integer will contain 5 characters filling empty spaces with zeroes.

System.out.printf("Integer : %05d", 10);

The output will be

Integer : 00010

To output floats, you will use %f.

System.out.printf("Float : %f", 2.456);

The output will be

Float : 2.456000

Here we can see that precision by default is up to 6 decimal places. Now let’s limit it to 2 decimal places

System.out.printf("Float : %.2f", 2.456);

The output will be

Floats : 2.46

Now let’s set the width of the float to 5 and also add a flag 0.

System.out.printf("Float : %05.2f", 2.456);

The output will be

Float : 02.46

Format a Boolean with printf()

Now let’s format boolean values. If the input is null or false, the output will be false otherwise, the output will be true.

System.out.printf("%b%n", true);
System.out.printf("%b%n", false);
System.out.printf("%b%n", "");
System.out.printf("%b%n", 1);
System.out.printf("%b%n", "Welcome to Apps Developer Blog");
System.out.printf("%b%n", null);

The output will be

true
false
true
true
true
false

Note that here %n inserts a new line character.

Format Date and Time with printf()

Now let’s learn to format date and time. Formatting date and time using printf() require %t or %T with a special character that represents a particular part of date and time. Here’s a list of a few special characters and the details of what it represents.

Special Characters Detail
H Hours
M Minutes
S Seconds
L Milliseconds
N Nanoseconds
p a.m/p.m
z time zone offset
A Full name of the day
d Two-digit day of the month
B Full name of the month
m The two-digit month of the year
Y Year in four digits
y Last two digits of the year

Now let’s have a look at examples.  The following snippet prints time by extracting hours, minutes, and seconds from the date object.

Date date = new Date();
System.out.printf("%tH:%tM:%tS%n", date, date, date);

The output will be

20:09:40

The following code snippet prints the name of the current month.

Date date = new Date();
System.out.printf("Month: %tB", date);

The output will be

Month: August

Conclusion

With this, we have come to the end of our tutorial. In this tutorial, we learned formatting output using printf() in java. Stay tuned for some more informative tutorials coming ahead, and feel free to leave any feedback in the comments section.

Happy learning!