Java Formatter format()
September 21, 2021
On this page we will walk through Java Formatter.format
method. The java.util.Formatter
class works as an interpreter for printf-style format strings. The Formatter
can perform layout justification, alignment, common formats for numeric, string and date/time data. The Formatter
produces locale specific output.
To format data,
Formatter
has format()
method as following.
a.
Formatter format(String format, Object... args)
2. Returns this formatter.
3. There is
IllegalFormatException
if format string contains illegal syntax, incompatible format specifiers or insufficient arguments.
4. If there are more than required arguments, the extras will be ignored.
b.
Formatter format(Locale l, String format, Object... args)
In this method, we can specify locale to format string and rest are same as in the first method.
Now here we will discuss initializing
Formatter
, format syntax and examples with format()
method.
Contents
1. Initialization of Formatter
Find some of the constructors ofFormatter
class.
1.
Formatter()
Formatter
with default locale and default charset for this instance of JVM. The destination of the formatted output is StringBuilder
.
2.
Formatter(Locale l)
Formatter
with specified locale and default charset for this instance of JVM. The destination of the formatted output is StringBuilder
.
3.
Formatter(File file)
4.
Formatter(File file, Charset charset, Locale l)
2. Format Syntax and Examples
Find the format syntax used byFormatter
class.
a. For general, character, and numeric types.
%[argument_index$][flags][width][.precision]conversion
flags: Set of characters that modify the output format. It is optional.
width: Positive decimal integer indicating the minimum number of characters to be written to the output. It is optional.
precision: Positive decimal integer used to restrict the number of characters. It is optional.
conversion: A character indicating how the argument should be formatted. It is required.
b.: For dates and times.
%[argument_index$][flags][width]conversion
%[flags][width]conversion
2.1 Conversions
Conversions are divided into categories that are general, character, numeric, date/time, percent and line separator.Find some conversions.
%s For string data.
%d For integer numbers.
%f For floating numbers.
%c For Unicode character.
%t Prefix for date and time.
The date/time conversion suffix are m for month, e for day of month, y for year, H for hour of the day in 24-hour clock, M for minute and S for second.
Date conversions can be used as %tm, %te, %ty, %tH, %tY, %tS .
Example:
1.
Formatter formatter = new Formatter(Locale.US); String name = "Krishn"; int age = 30; formatter.format("User name is %s and age is %d", name, age); System.out.println(formatter);
User name is Krishn and age is 30
Formatter formatter = new Formatter(Locale.US); Calendar c = new GregorianCalendar(); formatter.format("Birth date: %tm %te,%tY", c, c, c); System.out.println(formatter);
Birth date: 09 21,2021
2.2 Argument Index
The argument index is a decimal integer that denotes the position of the argument in the argument list. The argument index referenced by 1$ is for first argument and 2$ is for second argument and so on.Example:
1.
Formatter formatter = new Formatter(Locale.US); Formatter f = formatter.format("Numbers: %3$d %1$d %2$d", 10, 20, 30); System.out.println(f);
Numbers: 30 10 20
Formatter formatter = new Formatter(Locale.US); Calendar c = new GregorianCalendar(); Formatter f = formatter.format("Birth date: %1$tm %1$te,%1$tY %1$tH:%1$tM:%1$tS", c); System.out.println(f);
Birth date: 09 21,2021 09:32:26
2.3 Flags
'-' The result will be left-justified.'#' The result should use a conversion-dependent alternate form.
'+' The result will always include a sign.
' ' The result will include a leading space for positive values.
'0' The result will be zero-padded
',' The result will include locale-specific grouping separators.
'(' The result will enclose negative numbers in parentheses.
Example:
1.
Formatter formatter = new Formatter(); formatter.format(Locale.GERMAN, "e = %+f", Math.E); System.out.println(formatter);
e = +2,718282
Formatter formatter = new Formatter(); formatter.format(Locale.ENGLISH, "N = %(,d", -123456); System.out.println(formatter);
N = (123,456)
2.4 Width
The width is the minimum number of characters to be written to the output.Example:
Formatter formatter = new Formatter(); String str = "AB"; formatter.format(Locale.GERMAN, "|%5s|", str); System.out.println(formatter);
| AB|
2.5 Precision
For general conversions like %s, the precision is the maximum number of characters to be written to the output.For floating-point conversions like %f, the precision is the number of digit after radix point.
For character, integral and date/time conversions, the precision is not applicable.
Example:
1.
Formatter formatter = new Formatter(); String str = "ABCDE"; formatter.format(Locale.GERMAN, "Data: %.3s", str); System.out.println(formatter);
Data: ABC
2.
Formatter formatter = new Formatter(); formatter.format(Locale.FRANCE, "%2$s = %1$+5.2f", Math.E, "Formatted E"); System.out.println(formatter);
Formatted E = +2,72