Formatting With printf() In Java

May 17, 2022
blog image xperti 10


The println() method from PrintStream class is mainly used for printing in Java but there are some other methods as well that can be used for printing. The method you use depends on what type of output you want. In this article, we will be discussing another very useful printing method in Java called printf and how it can be used for formatting.

Java printf method

The printf method in Java can be used to output a formatted string to the console using various format specifiers. It is also an overloaded method of the PrintStream class. The printf method behaves the same as the invocation of the format() method.

explore new Java roles

Syntax of printf in Java

Following are the syntaxes available for overloading the printf method in Java.

Any one of these overloaded methods can be used to format the output:

System.out.printf(format, arguments);
System.out.printf(locale, format, arguments);

The method returns the output stream and it accepts up to three parameters depending on the overloading. The string parameter is simple as the printIn() method. The second line would be used to format the output where the argument would be the string that we want to format and the format parameter determines the format.

Format specifiers

We specify the format specifiers using the format parameter. The Format parameter is the combination of literals and format specifiers, always starting with the ‘%’ character.

Following the ‘%’ sign, a format specifier includes flags, width, precision, and conversion characters in the sequence mentioned below:


Although, all the specifiers mentioned in the brackets are optional. Most commonly a format specifier consists of % accompanied with a conversion character.

  • A conversion character determines how the argument will be formatted.
  • The <flags> define the standard ways for modifying the output. These are most common for formatting integers and floating-point numbers.
  • The <width> specifies the length of the field for printing the argument. It represents the minimum number of characters to be printed.
  • The <.precision> specifies the number of digits of precision when printing a floating-point value. It can also be used to define the length of a substring from a string.

Following are some of the most commonly used specifiers in the printf method in Java:

Specifier Explanation
%c Format characters
%d Format decimal (integer) numbers (base 10)
%e Format exponential floating-point numbers
%f Format floating-point numbers
%i Format integers (base 10)
%o Format octal numbers (base 8)
%s Format string of characters
%u Format unsigned decimal (integer) numbers
%x Format numbers in hexadecimal (base 16)
%n add a new line character

Formatting a Boolean value

To format Boolean values, we use the %b format specifier.  It works in such a way that if the argument is null, then the output will be “false”. If an argument is a Boolean value, then the output will the Boolean value already assign to it. Else, the output will be “true”.

See this example code:

1.  public static void main(String[] args) {                     
2.    boolean adult = true; 
3.    boolean member = false
4.    System.out.printf(“%b%n”, adult);
5.    System.out.printf("%b%n", null);
6.    System.out.printf("%b%n", "random text");
7.    System.out.printf("%b%n", member);
8. }

The output will be:


Formatting a string

When it comes to formatting strings using printf in Java, we have the %s specifier. It is usually combined with other specifiers depending on the formatting requirements. There are more things you can do with %s like using ‘%s’ to put the argument in single quotations or you can make the argument string uppercase, just by using the uppercase ‘S’ in %S.

See this code snippet:

System.out.printf("%s%n", "hello world!");
System.out.printf("'%S' %n", "hello world!");

And the output will be:

hello world!

The optional specifiers that include width, flag, and precision can also be used for formatting the string. To specify a minimum length, we can specify the length of the string using the width specifier. The blank spaces will be added to the left of the string if the argument string length is less than the minimum length.

System.out.printf("'%10s' %n", "Hello");

Which gives us:

 '  Hello'

If you need to left-justify the string, the flag can be used with a -ve sign like this:

System.out.printf ("'%-10s' %n", "Hello");

This will be the output:

'Hello     '

Lastly, you can also limit the number of characters in the argument string by specifying a precision, If we consider the syntax, %x.ys, The number x defines the padding on the left and y is the number of characters in the string.

For example:

System.out.printf("'%3.5s'", "Hello World!");

The output will be:

'   Hello'

Formatting numbers using Java printf

· Formatting Integers

The Java printf method can be used to format all types of integers available in Java which includes, byte, short, int, long, and BigInteger with the use of %d specifier.

System.out.printf("it is an integer: %d%n", 10000);

With the help of the d character, we’ll have this result:

it is an integer: 10000

Just like in string formatting, the flag specifier can be used for integers formatting and the output can also be formatted for different locales by overloading the method with the local parameter.

For example:

System.out.printf(Locale.US, "%,d %n", 12300);
System.out.printf(Locale.ITALY, "%,d %n", 10000);



It is to be noted that despite using the ‘,’ with Italy format you will still see the ‘.’ Separator based on the locale value.

· Formatting floating numbers and doubles

To format a float number, you have to use the %f specifier.

See this line of code:

System.out.printf("%f%n", 3.1423);

which will output:


As you can see, the number of decimal places by default is always 6 decimal places.

But it can be formatted by using the precision specifier like this:

System.out.printf("'%3.2f'%n", 3.1423);

Now the minimum width of our floating number will be 3, and the length of the decimal part will be 2 so the output will be:


To have your output in form of scientific notation, you can use the ‘e’ conversion character:

System.out.printf("'%3.2e'%n", 3.1423);

And this will be the output:


Date and time formatting

Java printf method deals with date, time, and date-time values for Date/time formatting. The conversion characters consist of two characters: the t/T character accompanied with the conversion suffix.

· Formatting time

For formatting time using Java Printf, H, M, and S characters are used for extracting the hours, minutes, and seconds from the input Date value. L and N characters represent the time in milliseconds and nanoseconds accordingly. p character is used to add a.m./p.m. formatting and lastly, z prints out the time-zone offset.

The data type variable consists of the complete data and time. The time formatting is used to output the time part from the Date in different formats.

See this code snippet below:

1.Date date = new Date();
2.System.out.printf("%tT%n", date);

These lines of code will give the following output:


This is a simple format for time but if you need to further change the format, different time segments can also be extracted separately using the characters mentioned before,

System.out.printf("hours: %tH%n minutes: %tM%n seconds: %tS%n", date, date, date);

The output will be:

hours: 13
minutes: 51
seconds: 15

· Formatting dates

Similar to time formatting, there are some special formatting characters used for formatting dates:

Characters Usage
A prints out the full day of the week.
d formats a two-digit day of the month.
B Prints the full month name.
m formats a two-digit month.
Y outputs the year in four digits.
y outputs the last two digits of the year.

Using these characters, this line of code:

System.out.printf("%1$tA, %1$tB %1$tY %n", date);

Will output the date like this:

Monday, June 2021

And to have your date in fully numeric format, separated with a ‘-‘, you can write your code like this:

System.out.printf("%1$td-%1$tm-%1$ty %n", date);

And the output will be:


You can play around with these characters, and by applying different combinations, easily output a unique date format using any separator of your choice.

See Also: A Guide To The Static Keyword In Java

Wrapping up

We discussed how to use the printf method in Java to format date/time, strings, numbers, and boolean values to be printed. There is a list of possible options made available in Java so that developers can easily format the output based on their needs and requirements of the program. It would eventually help Java developers in making their program more friendly and convenient for end-users.

new Java jobs



Candidate signup

Create a free profile and find your next great opportunity.


Employer signup

Sign up and find a perfect match for your team.


How it works

Xperti vets skilled professionals with its unique talent-matching process.


Join our community

Connect and engage with technology enthusiasts.